Created
August 12, 2022 13:11
-
-
Save viktordw/2699d143286b7bdd459ce7ffbbed28da to your computer and use it in GitHub Desktop.
Check public IP address of server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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