3

Is there a command line way to show the outward-facing IP address for my machine?

3 Answers 3

7

You could put together a script which calls whatismyip.com or similar and returns the interesting part (although such websites might not like you doing that). Generally, you're going to have to ask someone else (other computer) what your IP is, since your computer does not know. You could also possibly ask your router, but the parsing will probably be harder.

Your computer does not know it's external IP address because the router is using NAT*. A packet leaving your computer has from:[your internal IP], but the router mangles that into from:[Your router's external IP] and sends it onto the internet. The router then unmangles (or remangles) the to: field on the returned packets and forwards them to your computer.**

You can get a nearly-clean output from:

wget -q -O - checkip.dyndns.org

*Network address translation.

**This is only a hand-wavey description of how NAT works.

1
  • Adding a little sed to parse it: wget -q -O - checkip.dyndns.org | sed 's/.*: (.*)<\/body.*$/\1/'
    – jon077
    Apr 14, 2011 at 3:31
4

You can get it from various websites out there in the world, like checkip.dyndns.org. Once you have a site that tells you your IP address, it shouldn't be too hard to use curl to fetch the page and awk to parse it.

curl http://checkip.dyndns.org/ 2> /dev/null | ruby -pe '$_=$_.scan(/\d+\.\d+\.\d+\.\d+/)'
2

ifconfig.me has some great cmd line options.

This returns your ip

curl ifconfig.me/ip

This returns a large chunk of info

curl ifconfig.me/all