Skip to content

Instantly share code, notes, and snippets.

@bglopez
Created August 4, 2020 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bglopez/e5a472236ad1e305da93473db9eb123b to your computer and use it in GitHub Desktop.
Save bglopez/e5a472236ad1e305da93473db9eb123b to your computer and use it in GitHub Desktop.
Get IP address of Windows 10 host through WSL2. MobaXterm's X11 forwarding seems to work only when you set the DISPLAY variable to the LAN IP address of the Windows host itself and not the VM IP or 127.0.0.1.
#!/usr/bin/env bash
# example output
# regex in mywinip can be replaced with the following to snag more private subnets:
# '^.*(192\.168|10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.).*'
# but it *will* catch the 172 address assigned to the VM/as gateway as well
#
# mygw: 172.20.192.1
# mywinhost: DESKTOP-N2VKDIR
# mywinip: 192.168.0.39
mygw=$(cat /etc/resolv.conf | grep -v '^#' | grep nameserver | awk '{print $2}')
mywinhost=$(host ${mygw} | grep -Po '^.*pointer \K(.*)' | tr -d '.')
mywinip=$(host ${mywinhost} | awk '{print $4}' | grep -E '^.*(192\.168\.0).*')
printf "mygw: %s\nmywinhost: %s\nmywinip: %s\n" "${mygw}" "${mywinhost}" "${mywinip}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment