public
Created

  • Download Gist
getip.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/bin/bash
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 -n 1`
fi
 
if [ "$IPADDR" = "" ]
then
IPADDR=`cat /etc/hostname | head -n 1`
fi
echo $IPADDR

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.