Skip to content

Instantly share code, notes, and snippets.

@evilbuck
Created March 15, 2021 22:09
Show Gist options
  • Select an option

  • Save evilbuck/df61b74040d5df0046bb6ac934accdf1 to your computer and use it in GitHub Desktop.

Select an option

Save evilbuck/df61b74040d5df0046bb6ac934accdf1 to your computer and use it in GitHub Desktop.
@ECHO OFF
SET LXDISTRO=WLinux & SET WSL2PORT=3000 & SET HOSTPORT=3000
NETSH INTERFACE PORTPROXY RESET & NETSH AdvFirewall Firewall delete rule name="%LXDISTRO% Port Forward" > NUL
WSL -d %LXDISTRO% -- ip addr show eth0 ^| grep -oP '(?^<=inet\s)\d+(\.\d+){3}' > IP.TMP
SET /p IP=<IP.TMP
NETSH INTERFACE PORTPROXY ADD v4tov4 listenport=%HOSTPORT% listenaddress=0.0.0.0 connectport=%WSL2PORT% connectaddress=%IP%
NETSH AdvFirewall Firewall add rule name="%LXDISTRO% Port Forward" dir=in action=allow protocol=TCP localport=%HOSTPORT% > NUL
ECHO WSL2 Virtual Machine %IP%:%WSL2PORT%now accepting traffic on %COMPUTERNAME%:%HOSTPORT%
@evilbuck
Copy link
Author

This is to allow accessing ports within the wsl2 container from outside the local machine, or from another docker container hosted on the same machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment