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 respond in plain text. They also change quite frequently (go down, change their name, put up ads, output format might change etc.).
- The DNS response protocol is standardised (the format will stay compatible).
- 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.
- (for those geeks that care about micro-optimisation), this method should be inherently faster (be it only by a few micro seconds).
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: