A vast space of interconnected lines that represents the Internet.

Get Your Public IP Address with Ident.me

  • Adam Douglas

More often than not I find having to get my public Internet protocol address, and I’m pretty sure I’m not the only one. There are all kinds of services offering ways to show you your own public IP address. What most do not realize is that the majority of such websites, make it more complicated than it needs, over load the web page with advertisements, and distractions. Why not use something that is open source, simple to the point, and works in just about any application of use required?

The service I’m suggesting is called Ident.me, and provides the service to fetch a public IP address for free by donations. Requesting for a public IP address can be achieved using the following protocols, HTTP(S), DNS, SSH, telnet(s), and STUN. The IP address is sent in plain text format or in json format.

Get Public IP via a Web Browser

Using a web browser just simply visit one of the following web addresses. There’s nothing more to.

If you want to get more specific you can request to get the public IP address in different formats, and even with geolocation data.

Get Public IP via Command Line

Here are simple one line command examples to request for your public IP address.

$ curl https://ident.me/
$ dig +short @ident.me
$ nc ident.me 23
$ openssl s_client -quiet -connect ident.me:992
$ wget -qO- https://ident.me/
-q      Quite mode, turn off output
-O-     Display results to standard output

Make the JSON (JavaScript Object Notation) output format easier to read by using the jq command.

$ curl https://ident.me/json | jq
{
  "ip": "10.10.0.1",
  "aso": "MY-ISP",
  "asn": "27299",
  "continent": "NA",
  "cc": "CA",
  "country": "Canada",
  "city": "Example",
  "postal": "",
  "latitude": "45.4",
  "longitude": "-75.666667",
  "tz": "America/Example"
}

Get Public IP via Bash Script

The Bash script example below will attempt to fetch the public IP address using dig, curl, or wget.

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
#!/usr/bin/env bash
ip_host="ident.me"
timeout=2

getPublicIP() {
    if [ -x "$(command -v dig)" ]; then
        public_ip="$(dig +timeout=$timeout +tries=1 +short @$ip_host)"
    elif [ -x "$(command -v curl)" ]; then
        public_ip="$(curl -sL --max-time $timeout -w '\n' https://$ip_host)"
    elif [ -x "$(command -v wget)" ]; then
        public_ip="$(wget -T $timeout -qO- https://$ip_host)"
    fi
}
run() {
    getPublicIP
    showPublicIP
}
showPublicIP() {
    if [ -z $public_ip ]; then
        printf "%s" "dig, curl, and wget commands were not found. Unable to get public IP address."
    else
        printf "Public IP is %s" "$public_ip"
    fi
}

run

This is post 81 of 100, and is round 2 of the 100 Days To Offload challenge.

    • fix 100DaysToOffload post count
    • change 100DaysToOffload message
    • Add command examples
    • Fix Bash script
    • Correct spelling
    • Fix title heading
    • Add reference links