Quickly Get an External IP Address from the Command Line
Need to quickly get your external IP address from the command line for SSH or otherwise? No sweat, you can use either the curl command or dig to extract the information quickly from a variety of sources. We’ll focus on two different options that have proven to be reliable over time, the first is quite short and easy to remember, but the latter option may be considered the most reliable.
Or use the following:
dig +short myip.opendns.com @resolver1.opendns.com
The response with either string will be nothing but your external IP address, which is what the outside world sees, different from your LAN IP.
The dig command string is obviously quite a bit longer, but OpenDNS is a very reliable service that does much more than just retrieving IP information, so it may be considered a more reliable longterm solution, and thus it’s probably what is best to base any shortcuts or scripts on.
If you find yourself frequently needing your external IP, create a bash alias out of the aforementioned dig command, or you can turn to the menubar with the help of a free third party app that does nothing but keep your external IP address in the menu bar of OS X.
To create a quick IP retrieval command with using bash aliasing, paste the following line into your .bash_profile:
alias getmyip='dig +short myip.opendns.com @resolver1.opendns.com'
Save changes to .bash_profile, and now you will only need to type ‘getmyip’ to use the entire lengthy command string. Heads up to CommandLineFu for the dig string.
Note that you can also go to various websites like ifconfig.me and whatismyip.org through any web browser, be it lynx from the terminal, or Safari and Chrome in the GUI, to get an external IP that way as well. These commands will work the same in Mac OS X or Linux, whereas the browser based approach will work in quite literally anything capable of running a web browser.