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.).
- 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).
digwith OpenDNS as resolver:
dig +short myip.opendns.com @resolver1.opendns.com
Perhaps alias it in your
bashrcso it's easy to remember
alias wanip='dig +short myip.opendns.com @resolver1.opendns.com'
Responds with a plain ip address:
$ wanip 22.214.171.124