Journal

Writing down the things I learned. To share them with others and my future self.

28 Oct 2020

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
curl https://api.ipdata.co?api-key=test

{
    "ip": "88.71.116.156",
    "is_eu": true,
    "city": "Neuburg",
    "region": "Rheinland-Pfalz",
    "region_code": "RP",
    "country_name": "Germany",
    "country_code": "DE",
    "continent_name": "Europe",
    "continent_code": "EU",
    "latitude": 48.9821,
    "longitude": 8.25,
    "postal": "76776",
    "calling_code": "49",
    "flag": "https://ipdata.co/flags/de.png",
    "emoji_flag": "\ud83c\udde9\ud83c\uddea",
    "emoji_unicode": "U+1F1E9 U+1F1EA",
    "asn": {
        "asn": "AS3209",
        "name": "Vodafone GmbH",
        "domain": "vodafone.de",
        "route": "88.64.0.0/12",
        "type": "isp"
    },
    "carrier": {
        "name": "Vodafone",
        "mcc": "262",
        "mnc": "02"
    },
    "languages": [
        {
            "name": "German",
            "native": "Deutsch"
        }
    ],
    "currency": {
        "name": "Euro",
        "code": "EUR",
        "symbol": "\u20ac",
        "native": "\u20ac",
        "plural": "euros"
    },
    "time_zone": {
        "name": "Europe/Berlin",
        "abbr": "CET",
        "offset": "+0100",
        "is_dst": false,
        "current_time": "2020-10-28T20:51:58.377385+01:00"
    },
    "threat": {
        "is_tor": false,
        "is_proxy": false,
        "is_anonymous": false,
        "is_known_attacker": false,
        "is_known_abuser": false,
        "is_threat": false,
        "is_bogon": false
    },
    "count": "1561"
}

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
curl http://ipinfo.io                  
{
  "ip": "88.71.116.156",
  "hostname": "dslb-088-071-116-156.088.071.pools.vodafone-ip.de",
  "city": "Schuttertal",
  "region": "Baden-Württemberg",
  "country": "DE",
  "loc": "48.2667,7.9500",
  "org": "AS3209 Vodafone GmbH",
  "postal": "77978",
  "timezone": "Europe/Berlin",
  "readme": "https://ipinfo.io/missingauth"
}%                                   

DNS

The DNS based services are only returning the IP. One service is the google DNS:

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

OpenDNS also provide such a DNS name:

1
2
dig +short myip.opendns.com @resolver1.opendns.com
88.71.116.156

Tags