Skip to content

Instantly share code, notes, and snippets.

@viktordw
Created August 12, 2022 13:11
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 viktordw/2699d143286b7bdd459ce7ffbbed28da to your computer and use it in GitHub Desktop.
Save viktordw/2699d143286b7bdd459ce7ffbbed28da to your computer and use it in GitHub Desktop.
Check public IP address of server
#!/bin/bash
# NOTE: route command is part of dnsutils package on Debian, and net-tools on Ubuntu
# Based on following Stack Overflow answer:
# https://stackoverflow.com/questions/14594151/methods-to-detect-public-ip-address-in-bash/22209864#22209864
MAINIF=$( route -n | grep '^0\.0\.0\.0' | head -n 1 | awk '{print $NF}' )
IP=$( ifconfig $MAINIF | awk -v row=2 'FNR == 'row' {print $2}' )
echo $IP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment