这个问题与我关于将 WSL2 地址设为 static 的问题有关。由于这看起来不可能,我试图想出一个解决方法。
我想我可以在启动时在 WSL2 中运行一个 shell 脚本,它将 WSL2 机器的地址写入主机系统上的一个文件。powershell 脚本将查找该文件,并在找到该文件时运行:
netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=<WSL2 IP>
如果可能的话,我想消除在 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