How Do I Get The External IP Address?

 
 

I'd recommend getting it directly from a DNS server.

  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, more scalable, and generally more looked-after than whatever new hip whatismyip.com HTTP service is hot today.
  3. This method is inherently faster (be it only by a few milliseconds!).

Using dig with OpenDNS as resolver:

dig @resolver1.opendns.com ANY myip.opendns.com +short

Perhaps alias it in your bashrc so it's easy to remember

alias wanip='dig @resolver1.opendns.com ANY myip.opendns.com +short'

Responds with a plain ip address:

$ wanip
80.100.192.168 # or, 2606:4700:4700::1111

Syntax

(Abbreviated from https://ss64.com/bash/dig.html):

usage:  dig [@global-dnsserver] [q-type] <hostname> <d-opt> [q-opt]

    q-type   one of (A, ANY, AAAA, TXT, MX, ...). Default: A.

    d-opt    ...
             +[no]short          (Display nothing except short form of answer)
             ...

    q-opt    one of:
             -4                  (use IPv4 query transport only)
             -6                  (use IPv6 query transport only)
             ...

The ANY query type returns either an AAAA or an A record. To prefer IPv4 or IPv6 connection specifically, use the -4 or -6 options accordingly.

To require the response be an IPv4 address, replace ANY with A; for IPv6, replace it with AAAA.Note that it can only return the address used for the connection. For example, when connecting over IPv6, it cannot return the A address.

Alternative servers and examples

In addition to OpenDNS, there are similar DNS services provided by Akamai and Google:

$ dig @ns1-1.akamaitech.net ANY whoami.akamai.net +short
80.100.192.168

$ dig @ns1.google.com TXT o-o.myaddr.l.google.com +short
"80.100.192.168"

Example alias that specifically requests an IPv4 address:

alias wanip4='dig @resolver1.opendns.com A myip.opendns.com +short -4'

$ wanip4
80.100.192.168

And for IPv6:

alias wanip6='dig @resolver1.opendns.com AAAA myip.opendns.com +short -6'

$ wanip6
2606:4700:4700::1111

Troubleshooting

If the command is not working for some reason, there may be a problem with the upstream provider, the command-line tool, or something else. To help understand why it is not working, run the command without the +short option to reveal the details of the DNS query. For example:

$ dig @resolver1.opendns.com ANY myip.opendns.com

;; Got answer: ->>HEADER<<- opcode: QUERY, status: NOERROR

;; QUESTION SECTION:
;myip.opendns.com.      IN  ANY

;; ANSWER SECTION:
myip.opendns.com.   0   IN  AAAA    2606:4700:4700::1111

;; Query time: 4 msec
;; WHEN: Fri Apr 11 00:00:01 GMT 2011

 

If you have questions about it, any feedback on ServerSuit or just keep up with our updates, follow and @ us on Facebook and Twitter.

Until Next Time!

February 21 2020

Add or review comments

Please leave your comment

Existing comments

Comments 16


vfEneda
viagra vs cialis vs levitra which is better <a href="https://pcialis.com/">cialis professional</a> equivalent dose viagra cialis
BuDip
products like cialis <a href="https://cialnt.com/">cialis paypal viagra</a> cialis dolore gambe
DioBic
cialis pode viciar <a href="https://cialusorg.com/">cialis buy cialis without prescription</a> cialis holland bestellen
Grefly
buy cialis in ireland <a href="https://cialispos.com/">best liquid cialis</a> cialis douleurs reins
loans_On
merchant cash advance lenexa <a href="https://loanspersonala.com/">business cash advance roy</a> cash advances online sarasota
ret_ken
We accept BitCoin - <a href="https://creamretin.com/">retino a</a> what does cialis feel like
ret_ken
We accept BitCoin - <a href="https://creamretin.com/">tretinoin without prescription uk</a> buy generic cialis 5mg
ret_ken
We accept BitCoin - <a href="https://creamretin.com/">levitra diabetic retinopathy</a> prospect cialis
loansGek
cash advance lenders university city <a href="https://onlinequickloans.us.org/">Paydayloans Better Business Beaur</a> , <a href="http://cashadvanceonliner.com/">cash advances for debit cards</a> online payday loan lynn
loansGek
approved cash advance sarasota <a href="https://onlinequickloans.us.org/">Payday Loans Chapter 13 Bankruptcy</a> , <a href="http://cashadvanceonliner.com/">cash advance usa in hanford ca</a> credit card cash advance san mateo
DidHoazy
epimedium cialis <a href="https://focialisaf.com/">cialis 10mg price</a> genuine cialis online uk
DidHoazy
cialis for gastroparesis <a href="https://focialisaf.com/">generic cialis best price</a> zydena vs cialis
DidHoazy
why we use cialis <a href="https://focialisaf.com/">cialis 10mg price</a> cialis price vs viagra
DidHoazy
dosis correcta cialis <a href="https://focialisaf.com/">buy cialis in mexico</a> cialis 5 mg bestellen
Fibek
<a href="https://uscialisuk.com/">generic cialis uk</a> cialis negative effects <a href="https://bmzcialis.com">cialis online canada</a> / <a href="https://cialics.com/">cialis price rite aid</a>
Fibek
<a href="https://uscialisuk.com/">buy cialis in usa</a> diferencia cialis levitra viagra <a href="https://bmzcialis.com">buy cialis dubai</a> / <a href="https://cialics.com/">cialis dosage instructions</a>