Lookup Public IP via Commandline
When working from home, I have a changing public IP. My ISP assigns me a new public IP on every router reboot. For debugging I often have to know my current public IP. There are many websites you can visit and they will show you your public IP and advertisements.
To look it up from command line without advertisements or from a script, there are two main mechanism.
First there are REST services answering your request with the IP they see as client IP, secondly there are some DNS resolver
answering with the observed client IP if you query certain DNS records. In general the REST based service responses are easier
to parse since the answer is machine-readable. It’s very easy to parse the JSON output with jq
.
REST
Many REST services show more metadata they know about your IP. Mostly it is location data. This location data is very inaccurate. The most extensive response is from the ipdata.co IP. Using this API in a script may be a bad idea, since the API is rate limited. You can increase your rate limits by buying on of their plans.
|
|
The API of ipinfo.io is less extensive but I think they have no rate limit. As you can see from the example responses of ipinfo.io and ipdata.co, the location data is very inaccurate. Both location responses are in a 130km radius around my actual location.
|
|
DNS
The DNS based services are only returning the IP. One service is the google DNS:
|
|
OpenDNS also provide such a DNS name:
|
|