Skip to content

Instantly share code, notes, and snippets.

@DesktopECHO
Created November 10, 2020 15:18
Show Gist options
  • Save DesktopECHO/e05946d34c657942c1c660fcaf05167c to your computer and use it in GitHub Desktop.
Save DesktopECHO/e05946d34c657942c1c660fcaf05167c to your computer and use it in GitHub Desktop.
A simple CMD file to expose an open port in WSL2 to your entire LAN
@ECHO OFF
SET LXDISTRO=MyWSL2vm & SET WSL2PORT=3399 & SET HOSTPORT=3398
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%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment