This question is related to my question about making a WSL2 address static. Since that looks like it isn't possible I am trying to come up with a workaround.
I am thinking I can run a shell script in WSL2 at boot that will write the WSL2 machine's address to a file on the host system. A powershell script will look for that file, and when it finds it it will run:
netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=<WSL2 IP>
If possible I'd like to eliminate the need to run a shell script in Linux.
One possibility it via netsh interface ipv4 show neighbors
. 172.27.154.150 is the current ip address of my WSL2 machine, but I am not really sure how to write a script to isolate that IP address.
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
EDIT: See this answer for the script I wrote to automate starting SSHD in WSL and routing traffic to it