Finding external IP from command line

Lately, I was configuring Nagios and most of the systems were on the same private IP block as the Nagios server. One of the servers was outside the LAN and I was using the allowed_hosts directive for nrpe daemons where I needed to put the external IP of Nagios server in nrpe configuration of the outside host. I fiddled with elinks and it did not work which was a bit of frustration.

I thought about pinging the external host and capturing the icmp packets with tcpdump and many other ways came to mind. It turned out that I can find the external IP more easily using some of these ways.

The easiest way is to use is to use curl

curl www.whatismyip.org

Another way is to use wget by directing the output of standard output and sending the error messages to /dev/null

wget http://www.whatismyip.org -O - -o /dev/null

or if I wanted clearer output, I could use wget this way

wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' 

I could use elinks with checkip.dyndns.org and not with whatismyip.com

elinks checkip.dyndns.org

An additional tip. If you want to find all the IPs assigned to a system, the following gives you what you want

ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}

Hope this helps.

5 Responses to “Finding external IP from command line”

  1. external ip from the command line « Azitech Says:

    [...] Reference: http://linuxgravity.com/finding-external-ip-from-command-line [...]

  2. K Says:

    Umm..
    Why don’t you simply use ‘lynx http://www.whatsmyip.org‘ ?

    It’s standard and very easy.. =)

  3. Marnix A. van Ammers Says:

    Good and useful tips. Thanks. In Mac OS X, ifconfig output is slightly different. To show all IPs assigned in a Mac OS X system, the following works:

    ifconfig | awk ‘$1 == “inet” && $2 != “127.0.0.1″ {print $2}’

  4. David O'Dwyer Says:

    Simply use:

    curl icanhazip.com

  5. Anders Says:

    Your command about finding all IP address on your machine only gives IPv4 addresses, not IPv6.

    I usually use ‘ip add’ instead, as it is shorter and gives better information and can get/set much more information. Only needs to know one command :)

    To print out all IP addresses on the maching, I can use this version, which prints out both ipv4 and ipv6 addres:

    ip add show scope global | awk ‘/inet6?/ {print $2}’ | cut -d/ -f1

Leave a Reply