Wednesday, January 7, 2015

How can I get my external IP address in bash?

 How can I get my external IP address in bash?


I'd recommend getting it directly from a DNS server.
Most of the 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 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
 
Perhaps alias it in your bashrc so it's easy to remember
 
alias wanip='dig +short myip.opendns.com @resolver1.opendns.com'

Responds with a plain ip address:
$ wanip
80.100.192.168
 
found at: http://unix.stackexchange.com/questions/22615/... 

No comments:

Post a Comment

Cybermap

Internet Storm Center Infocon Status

Internet Storm Center Infocon Status
Internet Storm Center Infocon Status