09 May, 2015

Wan IP, external service WAN

Finding external IP using external services

The easiest way is to use an external service via a commandline browser or download tool. Since wget is available by default in Ubuntu, we can use that.
To find your ip, use-

 
wget -qO- http://ipecho.net/plain ; echo

You could also use lynx(browser) or curl in place of wget with minor variations to the above command, to find your external ip.
Using curl to find the ip:
 
curl ipecho.net/plain

For a better formatted output use:
 
curl ipecho.net/plain ; echo

A faster(arguably the fastest)
method using dig with OpenDNS as resolver:

The other answers here all go over HTTP to a remote server. Some of them require parsing of the output, or rely on the User-Agent header to make the server respond in plain text. They also change quite frequently (go down, change their name, put up ads, might change output format etc.).

  1. The DNS response protocol is standardised (the format will stay compatible).
  2. Historically DNS services (OpenDNS, Google Public DNS, ..) tend to survive much longer and are more stable, scalable and generally looked after than whatever new hip whatismyip.com HTTP service is hot today.
  3. (for those geeks that care about micro-optimisation), this method should be inherently faster (be it only by a few micro seconds).
Using dig with OpenDNS as resolver:
$ dig +short myip.opendns.com @resolver1.opendns.com
111.222.333.444
 


Finding external IP
without relying on external services

  • If you know your network interface name
Type the following in your terminal:
 
ifconfig  | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'

In the above, replace with the name of your actual interface, e.g:  

eth0, eth1, pp0, etc...

Example Usage:
 
mycomp@geek:~$ ifconfig ppp0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
111.222.333.444
  • If you don't know your network interface name
Type the following in your terminal (this gets the ip address of every network interface in your system):
 
ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } 
else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'

Example Usage:
 
mycomp@geek:~$ ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } 
else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'
lo: 127.0.0.1
ppp0: 111.222.333.444

No comments: