Skip to content

Instantly share code, notes, and snippets.

@piaoger
Created September 12, 2016 06:20
Show Gist options
  • Save piaoger/7a0d4c9013f8665402af5ed3df6f0788 to your computer and use it in GitHub Desktop.
Save piaoger/7a0d4c9013f8665402af5ed3df6f0788 to your computer and use it in GitHub Desktop.
get-public-ipv4.sh
# get public ipv4
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
# http://stackoverflow.com/questions/14594151/methods-to-detect-public-ip-address-in-bash
AMI_ID=$(curl --connect-timeout 5 "http://169.254.169.254/latest/meta-data/ami-id")
if [[ $AMI_ID == ami-* ]] ;
then
IPV4=$(curl --connect-timeout 5 "http://169.254.169.254/latest/meta-data/public-ipv4")
else
IPV4=$(curl --connect-timeout 5 "http://ipinfo.io/ip")
fi
echo "public-ivp4: $IPV4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment