DEV Community

tetractius
tetractius

Posted on

TetraQuicky00: Get your IP location from the command line

Well if you are reading this probably, you are looking at bash solution.

Bash

curl https://ipinfo.io/json

are you interested in specific information? By now you should now the easier way to parse JSON in the command line is jq. I personally suggest you install it with snap if you can.
So then you can do fancy stuff like

$ curl -s https://ipinfo.io/json | jq -r '.region'
Latium


bash

Powershell

Oh my god, really? Kudos!! Well, the good news is that you can do basically the same (assuming you are on pwsh 6 or 7):

curl -s https://ipinfo.io/json | jq -r '.region'
Latium

But if you don’t want to install an external tool like jq in windows, you can go native with something like:

Invoke-RestMethod http://ipinfo.io/json

Which returns you one of these strange .NET objects you can also dissect easily and do something like:

$(Invoke-RestMethod http://ipinfo.io/json).region
Latium

Special Tools if you want to avoid to go online too often

Well, there are interesting tools out there. One I was looking at is geoiplookup.
You can get it via the package managers of the distro and also via snap

Via SNAP

sudo snap install geoip-lookup

Via APT

sudo apt install geoip-bin

and you can do fancy things with local DBs reducing the number of requests:

geoiplookup `curl ifconfig.me`

And if you want to go more precise, without having to go and forth with external APIs you can do stuff like:

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
mv ./GeoLiteCity.dat /usr/share/GeoIP/
geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat `curl ifconfig.me`

So now you can quickly check if your encrypted delocalised VPN is working correctly by only doing:

curl https://ipinfo.io/json

after all, isn’t that is what is all about?

Top comments (0)