Skip to content

Instantly share code, notes, and snippets.

@Muaath5
Last active August 19, 2024 12:43
Show Gist options
  • Save Muaath5/85022d0063b9842826e43cd9660271f2 to your computer and use it in GitHub Desktop.
Save Muaath5/85022d0063b9842826e43cd9660271f2 to your computer and use it in GitHub Desktop.
WSL ipconfig

WSL ipconfig

Basic IP information command for WSL

Installation

sudo curl https://gist.githubusercontent.com/Muaath5/85022d0063b9842826e43cd9660271f2/raw/0530d123056d30a3c0b25fef8a2ed62390d7403e/ipconfig > ipconfig
sudo chmod +x ./ipconfig
sudo mv ./ipconfig /usr/bin/local

Usage

No input will print everything, you can use a specific one.

  • -1: Prints Local IP
  • -2: Prints WSL IP
  • -3: Prints Windows IP
  • -4: Prints Public IP
#!/bin/bash
# Function to get IP addresses
get_ips() {
case $1 in
-1)
hostname -I | cut -d' ' -f1
;;
-2)
awk 'NR==2' <<< "$win_ipconfig"
;;
-3)
awk 'NR==1' <<< "$win_ipconfig"
;;
-4)
curl -s https://api64.ipify.org
;;
*)
echo "Local IP: $(hostname -I)"
echo "WSL IP: $(awk 'NR==2' <<< "$win_ipconfig")"
echo "Windows IP: $(awk 'NR==1' <<< "$win_ipconfig")"
echo "Public IP: $(curl -s https://api64.ipify.org)"
;;
esac
}
# Windows IP Config
curpath=$(pwd)
cd /mnt/c
win_ipconfig=$(cmd.exe /c "ipconfig | findstr IPv4" | awk -F ": " '{print $2}')
cd $curpath
# Get IP addresses based on arguments
get_ips $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment