How to check your global IP address using commands (curl/dig, etc.)

table of contents
Hello everyone.
This is Naka from the Systems Solutions Department, and I have trouble getting out of bed when it gets cold.
While struggling to decide what to write on my blog, I suddenly
realized that "the method for checking one's global IP address varies slightly from person to person" and "the commands used also vary slightly."
So this time, I would like to focus on explaining how to check your global IP address using commands (curl/dig/nslookup)
However,
some of you may be wondering,
We will also explain how to do this using the most common browsers
Execution environment
● Linux environment
: OS: Ubuntu 20.04.5 LTS (WSL2 environment)
, Shell: bash
, Locale changed to Japanese
● Windows environment
: OS: Windows 11 (version: 21H2)
, Shell: Command Prompt
, Language setting changed to Japanese
How to check using a common browser
First, a general explanation of the method
Whether you're an infrastructure engineer or not,
to check the global IP address of your PC is
the most common way
*The two most well-known sites that come up at the top of search results are the ones below
○ "CMAN"
https://www.cman.jp/network/support/go_access.cgi
○ "Confirmation Tool - UGTOP"
https://www.ugtop.com/spill.shtml
When you access these IP verification sites, the global IP address you are currently using will be displayed
principle
The principle is simple
When a client (user) accesses a web server using a browser,
they send an HTTP request to the server using the HTTP(S) protocol.
In this case, the "HTTP request" sent by the browser is generally divided into four structures
(I will omit the details as this alone could fill an entire article).
- A "request line" that specifies the communication protocol and method
- A "header line" containing information about the requester
- Blank line
- Message body
called the "request header,"
of the client (user) that requested the accesswhich includes the IP addresscontains
In short, IP verification sites provide the user with the information from the request header they sent,
thereby revealing their global IP address.
This principle is also used in the commands described below, so please keep it in mind
How to check using the curl command (Linux & Windows)
● curl
When it comes to checking using a command, I'm guessing that 95 out of 100 people would probably use the curl command, which is a standard method
This command itself is a great command that can send and receive data using various communication protocols, and
it is generally used to check whether a web server is running correctly.
It's included by default in most major Linux distributions these days, and
it's also included by default in Windows 10 (from version 1803 (RS3) onwards, starting in 2018).
curl inet-ip.info xxx.xxx.xxx.xxx curl ifconfig.co xxx.xxx.xxx.xxx curl httpbin.org/ip { "origin": "xxx.xxx.xxx.xxx" } *Output in json format
The main usage is as above
You may have noticed that this involves sending a request to a website, and you're right;
as the method using "request headers" explained above.
The only slight difference is the domain name
I'm using the curl command to send an HTTP request to an IP address verification site,
and then I'm getting only the IP address back from the request header information.
There are several other sites with the same purpose, so the choice will depend on your preference for the domain name
I think the reason why it's "slightly different for each person" is because the sites people use to check it vary
In conclusion, please keep in mind that "curl is not a command that checks your global IP address internally."
It relies on an external service for this purpose.
How to check using the dig/nslookup command (Linux/Windows)
● dig (Linux)
● nslookup (Windows)
The familiar commands dig and nslookup are used to check domains
Although the two commands mentioned above have different names and use different tools,
their usage in this case is generally the same, so I will explain them together.
This command is a tool that queries DNS servers to get a response with DNS records, but
actually there are several DNS servers that will tell you your global IP address.
- Cisco (OpenDNS)
- Cloudflare
For example, if you make a specific query to the specific DNS server mentioned above, it will respond with the global IP address of the person making the query
These features are apparently intended for purposes such as checking whether a service using that particular DNS is being used, and they
don't seem to be widely known.
Upon reviewing official documents and employee forum posts from the company in question and its affiliates,
we found some brief descriptions of its existence and usage.
This verification method uses the DNS server of "Cisco (OpenDNS)," which we have determined to be publicly disclosed
●ubuntu(bash) dig myip.opendns.com @208.67.222.222 +short xxx.xxx.xxx.xxx
With "dig", you can use the "+short" option, which allows you to display only the global IP
●Windows(CMD) nslookup myip.opendns.com 208.67.222.222 Server: dns.opendns.com Address: 208.67.222.222 Non-authoritative answer: Name: myip.opendns.com Address: xxx.xxx.xxx.xxx
"nslookup" does not have a shortening option, but it is shorter than the full display of "dig" so it may be easier to read
This information was found in a knowledge base post by an employee on the Cisco community forum.
*While I added the `dig short` option, the core usage is the same as described in the knowledge base post.
Umbrella: Special DNS requests that can only be used by Umbrella DNS servers
Aside: Google DNS
By the way, Google actually has some information about this in their public documentation
However, this wasn't a command description that perfectly yielded the desired result, and
unlike Cisco, the response format is a TXT record, so the command used is a bit longer.
I will skip the explanation this time
Which is more useful: curl or dig & nslookup?
: "The client that executed the command (global IP address)."yield the same answer
So, there's no problem with using your preferred method
But this is not a satisfactory answer
In reality, I don't think there's a huge difference in quality, but if I had to point out a difference, that dig & nslookup is more stablewould say
The curl method will not produce results unless the web server you are querying is running
While writing this article, I accessed several IP address verification service websites, but some
displayed the error code "429 Too Many Requests,"preventing me from obtaining any results.
However, dig and nslookupDNSare methods for querying
Yes... the DNS servers managed by Google and Cisco (OpenDNS)are basically stable and reliable, so you can rest assured.
In fact, if they were to stop, it would be a disaster.
When considering the stability of a service when making inquiries,
a large corporation's DNS server is clearly more stable than a single web server.
Aside: Actually, dig and nslookup are faster internally
There is no noticeable difference, but dig and nslookup are faster in terms of "internal" execution speed
This is because curl uses TCP, which requires a response to the communication protocol, while
dig and nslookup use UDP, which sends data unilaterally, resulting in a speed difference.
Conclusion
"There's not a huge difference, so use the method that's easiest for you." "If I had to say, dig and nslookup are faster."
There are many situations where you will need to use this, such as checking the global IP address of a server, so I hope this article will be of some help to you
This was a long post, but thank you for reading this far!
Reference materials
HTTP Overview
https://developer.mozilla.org/ja/docs/Web/HTTP/Overview
curl shipped by Microsoft (official curl website)
https://curl.se/windows/microsoft.html
Umbrella: Special DNS requests that can only be used by Umbrella DNS servers
15
