Skip to content

Instantly share code, notes, and snippets.

@Spotlightsrule
Created August 29, 2021 07:36
Show Gist options
  • Save Spotlightsrule/34f3df10b2d56b193858fc223ae6f816 to your computer and use it in GitHub Desktop.
Save Spotlightsrule/34f3df10b2d56b193858fc223ae6f816 to your computer and use it in GitHub Desktop.
Default Gateway
Linux/Unix (for gateway): Determine which to use via "type netstat", skipping to the second if this check fails (contains the word "not found")
netstat -nr -f inet | grep "UG" | tail -1 | awk "{print $2}"
ip route | grep "^default" | head -1 | awk "{print $3}"
Linux/Unix (for base addr): Determine which to use via "type netstat", skipping to the second if this check fails (contains the word "not found")
netstat -nr -f inet | grep "U " | tail -1 | awk "{print $1}"
ip route | tail -1 | awk "{print $1}"
MacOS (for gateway):
netstat -nr -f inet | grep "^default" | head -1 | awk "{print $2}"
Windows (for cmd/W7 and up):
netstat -nr -p icmp | for /f "tokens=3" %a in ('findstr -R "0\.0\.0\.0.*0\.0\.0\.0"') do @echo %a
Windows (for powershell/W7 and up):
(Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway | select -first 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment