Skip to content

Instantly share code, notes, and snippets.

@zulhfreelancer
Last active May 8, 2024 01:54
Show Gist options
  • Save zulhfreelancer/c5365a5eb7de2e152b42328ea5c3ec7c to your computer and use it in GitHub Desktop.
Save zulhfreelancer/c5365a5eb7de2e152b42328ea5c3ec7c to your computer and use it in GitHub Desktop.
How to quickly get server's public IP address?

How to quickly get server's public IP address?

The curl / wget way

$ curl http://icanhazip.com
# or
$ wget -qO- http://icanhazip.com | xargs echo

Other services:


The dig / DNS Lookup way

Further reading: https://sslip.io

A big advantage of using DNS queries instead of HTTP queries is bandwidth: querying ns-aws.sslip.io requires a mere 592 bytes spread over 2 packets; Querying https://icanhazip.com requires 8692 bytes spread out over 34 packets — over 14 times as much! Admittedly bandwidth usage is a bigger concern for the one hosting the service than the one using the service.

dig @ns.sslip.io txt ip.sslip.io +short    # sample reply "2607:fb90:464:ae1e:ed60:29c:884c:4b52"
dig @ns.sslip.io txt ip.sslip.io +short -4 # forces IPv4 lookup; sample reply "172.58.35.231"
dig @ns.sslip.io txt ip.sslip.io +short -6 # forces IPv6 lookup; sample reply "2607:fb90:464:ae1e:ed60:29c:884c:4b52"

When querying for your IP address, always include the sslip.io name server (e.g. @ns.sslip.io). If omitted, you won't get your IP address; instead, you'll get the IP address of your upstream name server.

dns.toys also offer the same thing:

dig +short -4 ip @dns.toys # IPv4 address
dig +short -6 ip @dns.toys # IPv6 address

The ssh way

ssh sshmyip.com 2>&1 | grep "\"ip\"" | awk -F'"' '{print $4}'
# or
ssh telnetmyip.com 2>&1 | grep "\"ip\"" | awk -F'"' '{print $4}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment