-
-
Save jcanfield/8086065 to your computer and use it in GitHub Desktop.
Bash script for *nix systems to display your Public IP Address.
This file contains hidden or 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 | |
# Display Public IP Address | |
# USAGE: my_ip | |
# Other Commands: | |
# `wget -qO- ifconfig.me/ip` | |
# `curl ifconfig.me` | |
# `wget -qO- http://shtuff.it/myip/short` | |
echo "--> Displaying your Public IP Address.." | |
IPADDR="" | |
if [ -f /usr/bin/ec2metadata ] | |
then | |
IPADDR=`timeout 1 ec2metadata --public-hostname` | |
fi | |
if [ "$IPADDR" = "" ] | |
then | |
IPADDR=`curl http://www.jsonip.com 2>/dev/null | egrep -o "[0-9\.]*"` | |
fi | |
if [ "$IPADDR" = "" ] | |
then | |
IPADDR=`ifconfig | perl -ple 'print $_ if /inet addr/ and $_ =~ s/.*inet addr:((?:\d+\.){3}\d+).*/$1/g ;$_=""' | grep -v ^\s*$ | grep -v 127.0.0.1 | head $ | |
fi | |
if [ "$IPADDR" = "" ] | |
then | |
IPADDR=`cat /etc/hostname | head -n 1` | |
fi | |
echo $IPADDR | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment