Windows-WSL端口映射
windows设置脚本
- 放入:
win+r输入shell:startup再按回车键
的目录中wslname = "Ubuntu-20.04" '要转发端口的Linux系统名称,可以通过 wsl -l 命令查看
wslport = "80" 'Linux系统里要转发的端口
winport = "80" '转发到Windows上的端口
'以下代码无需修改
If WScript.Arguments.Length = 0 Then
'用于以管理员权限运行,不然没有权限配置端口转发
CreateObject("Shell.Application").ShellExecute "wscript.exe" _
, """" & WScript.ScriptFullName & """ RunAsAdministrator", , "runas", 1
WScript.Quit
End if
set fso = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")
writeip = objShell.Run("cmd /c ""wsl -d "+wslname+" -u root hostname -I"">C:\Windows\Temp\wslip.tmp",0,True)
set f = fso.OpenTextFile("C:\Windows\Temp\wslip.tmp", 1)
wslip = f.ReadAll()
f.Close()
command = "cmd /c ""netsh interface portproxy add v4tov4 listenport="+winport+" listenaddress=0.0.0.0 connectport="+wslport+" connectaddress="+wslip
forwarding = objShell.Run(command,0,True)
fso.deleteFile("C:\Windows\Temp\wslip.tmp")
放行windows防火墙
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Bai's Blog!