Skip to content

Instantly share code, notes, and snippets.

@jeroen
Created January 29, 2013 23:34
Show Gist options
  • Save jeroen/4669037 to your computer and use it in GitHub Desktop.
Save jeroen/4669037 to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment