Skip to content

Instantly share code, notes, and snippets.

@jcanfield
Forked from jeroen/getip.sh
Last active January 1, 2016 03:29
Show Gist options
  • Save jcanfield/8086065 to your computer and use it in GitHub Desktop.
Save jcanfield/8086065 to your computer and use it in GitHub Desktop.
Bash script for *nix systems to display your Public IP Address.
#!/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