How many times we need to gather simple informations manually? Wouldn’t be easier and simpler just to get it with a one-liner? Every user can get his your public ip opening a browser and typing “What is my ip?”, few users know how to get the same information in one line from the cli.
Using Curl
A very popular BaSH command is curl, recently available on Windows 10 (1803) cmd-prompt :
1 2 |
paolofrigo$ curl https://icanhazip.com 101.189.112.XXX |
As you can see is just one line. But we need curl to get that information? No we can use Powershell, if we use PowerShell Core we will be sure to get the same result on each OS.
Using PowerShell
We will use Invoke-RestMethod, which is included from PowerShell 3, we will connect to 3 public websites to compare the results and in the last example most importantly we can retrieve a JSON object that we can use or simply save.
The first website is icanhazip.com, it’s a funny name and it’s always easy to remember, at least for me
1 2 |
PS /Users/paolofrigo> Invoke-RestMethod -uri https://icanhazip.com/s 101.189.112.XXX |
The second one is the www.ipify.org, it’s a Simple Public Address API.
1 2 |
PS /Users/paolofrigo> Invoke-RestMethod -uri https://api.ipify.org 101.189.112.XXX |
The last one that I use is www.ipinfo.io :
1 2 3 4 5 6 7 8 9 10 |
PS /Users/paolofrigo> Invoke-RestMethod -uri https://ipinfo.io/json ip : 101.189.112.XXX hostname : cpe-101-189-112-XXX.vb04.vic.asp.telstra.net city : Brighton region : Victoria country : AU loc : -37.9056,145.0030 postal : 3186 org : AS1221 Telstra Pty Ltd |
Selecting just the IP:
1 2 |
Invoke-RestMethod -uri https://ipinfo.io/json | select-object -ExpandProperty ip 101.189.112.XXX |
Invoke-RestMethod and JSON objects
Using a RestAPI and storing the JSON object on a text file on a local directory.
1 2 3 4 5 6 7 8 9 |
PS /Users/paolofrigo> Invoke-RestMethod -uri https://ipinfo.io/json -outfile externalip.json PS /Users/paolofrigo> get-content ./externalip.json { "ip": "101.189.112.XXX", "hostname": "cpe-101-189-112-XXX.vb04.vic.asp.telstra.net", "city": "Brighton", "region": "Victoria", "country": "AU", "loc": "-37.9056,145.0030", "postal": "3186", "org": "AS1221 Telstra Pty Ltd" } |
In case you want a similar result of the curl example, using the Invoke-RestMethod alias will be ever more compact:
1 2 |
PS /Users/paolofrigo> irm http://icanhazip.com 101.189.112.XXX |
Do you need IPv4 and/or IPv6?
I suggest you to use icanhazip, with 2 subdomains ipv4 and ipv6
1 2 3 4 5 |
PS /Users/paolofrigo> Invoke-RestMethod -uri http://ipv4.icanhazip.com 101.189.112.XXX PS /Users/paolofrigo> Invoke-RestMethod -uri http://ipv6.icanhazip.com 2001:8003:4e67:1c00:d8af:1557:6ca1:XXXX |
Quick Update
- I’m adding another website as suggested by Daniel F. to obtain your client IP: https://www.wizcase.com/tools/whats-my-ip/
- On this subject, just a few months ago I’ve created my personal URL shortener called JSYK.IT (which is a simple acronym – Just So You Know IT) and if you want to obtain your public IP you can just type /IP: https://jsyk.it/ip