Last Updated: February 25, 2016
·
2.432K
· ashrocket

How to get your External IP from bash

tldr;

ec2-authorize sg-xyyyxx --region=ap-southeast-2 -p 0-65535 -s `curl ifconfig.me`/32

I have been using a script to grab external ip.

echo "wget http://checkip.dyndns.com/ -O - -o /dev/null | sed -n 's/.*\(Current.*\)<\/body\>.*$/\1/p' " > ~/.bin/extip; chmod ugo+x ~/.bin/extip
extip
>Current IP Address: 63.999.999.888

http://checkip.dyndns.com/ Is a great service from the folks at dyndns.com

or even better just:

curl ifcong.me
>63.999.999.888

I often need to temporarily adjust a security group from Amazon and getting this info on the command line is very useful for me.

Additional steps might include:

#Set you region and find the security group you are interested in changing.
ec2-describe-group --region=ap-southeast-2
ec2-describe-group --region=ap-southeast-2 | grep sg-xyyyyxx
# Remove any old holes you punched in your firewall
ec2-revoke sg-xyyyxx --region=ap-southeast-2 -p 0-65535 -s 63.999.999.999/32 
# Punch a new hole in your firewall
ec2-authorize sg-xyyyxx --region=ap-southeast-2 -p 0-65535 -s 63.999.999.888/32

>GROUP  sg-xyyyxx               
>PERMISSION         ALLOWS  tcp 0   65535   FROM    CIDR    63.999.999.888/32   ingress

UPDATE: user infoslack recommended using
curl ifconfig.me

Which I find to be even more brilliant since it returns * only the ip address * we're looking for. In combination with the bash shell's tick operator ` we can recombine our ec2-authorize command to a single line.

ec2-authorize sg-xyyyxx --region=ap-southeast-2 -p 0-65535 -s `curl ifconfig.me`/32

3 Responses
Add your response

Nice!
ifconfig.me know ?

Try:
curl ifconfig.me

over 1 year ago ·

Thanks! I like ifconfig.me even better than http://checkip.dyndns.com because it returns just the value, which could be easily used in combination with other commands.

over 1 year ago ·

dig +short myip.opendns.com @resolver1.opendns.com

over 1 year ago ·