How to Get Public IP Address in Linux

Problem with WGET/CURL

You might have a seen a lot of wget/curl related way to get an IP address. One of my clients was using it and it looks like somehow he was blocked. Now it might have not been the case if he was spoofing but assume that he would get blocked eventually, I used dig with opendns (over Google).

Blocking isn’t one way – may be the service is down – it happened to us all isn’t it?

With dig, system is running for a year and my Client hired me for another project and asked me how I did it. So I thought I should share it to readers as well.

Most Reliable way is through dig

dig +short myip.opendns.com @resolver1.opendns.com
dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{ print $2}'

A little less reliable way is through host

host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}'
How to Get Public IP Address in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top