IP geolocation is the process of mapping a user’s IP address to its physical location. Whenever a user visits a website, that website can read their IP address. By querying a geolocation database or API, the website can obtain the user’s country, region, city, latitude, longitude and so much more. Websites use geolocation to provide a multitude of functionalities to their users such as marketing, advertising, content customization and so on. IP2Location.io has a variety of libraries that can easily be integrated into websites. But how about if I just want to query geolocation on a command line? Well, now you can use the IP2Location.io CLI written in the Go programming language to query an IP address for its geolocation. With this CLI tool, you can easily check geolocation for IP addresses in log files and much more.
Below, we’ll show to install the IP2Location.io CLI tool as well as the prerequisites. Then, we’ll show you some of the usage so that you can easily get started. Our demo machine is a Debian 12 instance so some of the steps will be for that platform.
- You will need the IP2Location.io API key. So, do sign up for the free plan at http://www.ip2location.io/pricing and take note of the API key given.
- Install Go for your platform. Follow the instructions for your platform at https://go.dev/dl/.
If you’re using Debian 12, then you can follow these steps to install Go, otherwise refer to the above link for the Go installation steps.
sudo apt update sudo apt install golang
Installing the IP2Location.io CLI in Go
Once you’ve installed Go, you can just run the below command to install the IP2Location.io CLI.
go install github.com/ip2location/ip2location-io-cli/ip2locationio@latest
Configure the path to the IP2Location.io CLI
Run the below command to see which path is being used to store the IP2Location.io CLI executable.
Look for the GOPATH variable, which in our case is /home/admin/go so our installed executable is in /home/admin/go/bin folder.
Now you can just add /home/admin/go/bin into your PATH environment variable for your platform. In Debian, we’ll just add the path into the ~/.bash_profile by adding the below lines.
After saving the file, you can need to run the below to reload the path.
Now, you should be able to just call the IP2Location.io CLI without a path so let’s try a simple command below to test.
If your path has been set correctly, you’ll see the CLI’s version. Let’s explore some of the parameters we can use with CLI.
Display the help
Command line users rely on the help display to show them what parameters are required by a particular command. The IP2Location.io CLI can display the help in a similar fashion with the below command.
Configure the IP2Location.io API key
Before you can retrieve IP geolocation data, you will need to configure the CLI with the IP2Location.io API key. Run the below command to configure your API key.
ip2locationio config <API KEY>
Check my own public IP geolocation
Let’s see what IP geolocation data is returned for my own public IP address. Run the command and see.
The above is my public IP geolocation using the Security plan which contains all the available fields. You may see less fields depending on your API plan.
If you wish to see a pretty print version, you can add 1 parameter like below.
ip2locationio -o pretty
Query any IP address for the geolocation
The IP2Location.io CLI can accept either an IPv4 or an IPv6 address. Just try the below command.
Or do a pretty print with the below command.
ip2locationio -o pretty 22.214.171.124
Filter the result fields
Often times, we may just need a few fields from the result. You can easily filter the fields you wish to display like below. You can just give a comma separated list of the field names you wish to show. Nested fields can be called using periods.
ip2locationio -f country_code,region_name,city_name,continent.name,time_zone_info.olson 126.96.36.199
Specifying the language for translations
If you’re subscribed to the Plus or Security plans, then you have access to the translation data. Let’s say you wish to see the Spanish translation. You can just call the command like below.
ip2locationio -l es -f country_name,country.translation.value,region_name,region.translation.value,city_name 188.8.131.52
It is that simple to use the IP2Location.io CLI to get the IP geolocation for an IP address. Combine this with other commands and you can analyze data or build reports easily.
If you see the help display, you will notice that there are a few more commands at the bottom. We won’t cover those as they are not related to IP geolocation. But feel free to try them out as well.
THE POWER OF IP GEOLOCATION API
Find a solution that help in your business.