Nic*_*ick 18 networking powershell wsl2
这个问题与我关于使 WSL2 地址为 static 的问题有关。由于这看起来不可能,我正在尝试提出一种解决方法。
我想我可以在启动时在 WSL2 中运行一个 shell 脚本,它将 WSL2 机器的地址写入主机系统上的文件。powershell 脚本将查找该文件,并在找到后运行:
netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=<WSL2 IP>
Run Code Online (Sandbox Code Playgroud)
如果可能,我想消除在 Linux 中运行 shell 脚本的需要。
一种可能性它通过netsh interface ipv4 show neighbors
。172.27.154.150 是我的 WSL2 机器的当前 IP 地址,但我不确定如何编写脚本来隔离该 IP 地址。
PS C:\Users\Nick> netsh interface ipv4 show neighbors
Interface 1: Loopback Pseudo-Interface 1
Internet Address Physical Address Type
-------------------------------------------- ----------------- -----------
224.0.0.22 Permanent
239.255.255.250 Permanent
Interface 7: Ethernet0
Internet Address Physical Address Type
-------------------------------------------- ----------------- -----------
192.168.163.2 00-50-56-fa-e9-9e Reachable
192.168.163.254 00-50-56-fc-61-94 Reachable
192.168.163.255 ff-ff-ff-ff-ff-ff Permanent
224.0.0.22 01-00-5e-00-00-16 Permanent
224.0.0.251 01-00-5e-00-00-fb Permanent
224.0.0.252 01-00-5e-00-00-fc Permanent
239.255.255.250 01-00-5e-7f-ff-fa Permanent
255.255.255.255 ff-ff-ff-ff-ff-ff Permanent
Interface 19: Bluetooth Network Connection
Internet Address Physical Address Type
-------------------------------------------- ----------------- -----------
224.0.0.22 01-00-5e-00-00-16 Permanent
Interface 14: vEthernet (Default Switch)
Internet Address Physical Address Type
-------------------------------------------- ----------------- -----------
172.21.47.255 ff-ff-ff-ff-ff-ff Permanent
224.0.0.22 01-00-5e-00-00-16 Permanent
224.0.0.251 01-00-5e-00-00-fb Permanent
239.255.255.250 01-00-5e-7f-ff-fa Permanent
255.255.255.255 ff-ff-ff-ff-ff-ff Permanent
Interface 25: vEthernet (WSL)
Internet Address Physical Address Type
-------------------------------------------- ----------------- -----------
172.27.154.150 00-15-5d-f2-6b-94 Stale
172.27.159.255 ff-ff-ff-ff-ff-ff Permanent
224.0.0.22 01-00-5e-00-00-16 Permanent
224.0.0.251 01-00-5e-00-00-fb Permanent
239.255.255.250 01-00-5e-7f-ff-fa Permanent
Run Code Online (Sandbox Code Playgroud)
Has*_*own 35
对我来说,我的 WSL 有多个接口,所以hostname
不合适。在这种情况下你可以使用这个:
wsl -- ip -o -4 -json addr list eth0 `
| ConvertFrom-Json `
| %{ $_.addr_info.local } `
| ?{ $_ }
Run Code Online (Sandbox Code Playgroud)
更改-4
为-6
ipv6,并更改eth0
为您选择的接口。
另外,如果您需要虚拟机中主机的 IP 地址,您可以运行此命令(在主机上)
$wsl = wsl -- ip -o -4 -j addr s eth0 | ConvertFrom-Json | %{ $_.addr_info.local } | ?{ $_ }
$master = (Get-NetIPAddress).IPAddress | ?{ $_ -match ($wsl -replace '^((\d+\.){2}).*$','^$1') }
$wsl,$master #access wsl from master using the first ip, the converse from the latter
Run Code Online (Sandbox Code Playgroud)
woj*_*tow 31
从 Windows powershell 或 cmd 使用命令:
wsl hostname -I
Run Code Online (Sandbox Code Playgroud)
如果 WSL 正在运行,这应该返回一个 IP 地址。如果未运行,它似乎会启动默认发行版,然后返回该发行版的地址。(返回需要几秒钟的时间)
小智 15
要从WSL 计算机检查您的 WSL IP 地址:
ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21711 次 |
最近记录: |