How to obtain IP geolocation using the IP2Location.io CLI

Query IP geolocation using the IP2Location.io CLI

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.

NOTE: We will list out the various ways you can install the IP2Location.io CLI such as:

  • Installing using .deb file for Debian/Ubuntu users.
  • Installing from Ubuntu PPA for Ubuntu users.
  • Installing via PowerShell for Windows users.
  • Downloading the pre-built binaries for various OSes and architectures.
  • Installing via go install.

 Prerequisites

HOW TO INSTALL

Installing using .deb file for Debian/Ubuntu users

Check the latest release version at https://github.com/ip2location/ip2location-io-cli/releases/latest and update the commands below before running them.

curl -LO https://github.com/ip2location/ip2location-io-cli/releases/download/v1.1.0/ip2location-io-1.1.0.deb
sudo dpkg -i ip2location-io-1.1.0.deb

Installing from Ubuntu PPA for Ubuntu users

Run the below commands.

sudo add-apt-repository ppa:ip2location/ip2locationio
sudo apt update
sudo apt install ip2location-io

Installing via PowerShell for Windows users

Launch PowerShell as administrator. Check for the latest version at https://github.com/ip2location/ip2location-io-cli/releases/latest then update the command below before running it.

iwr -useb https://github.com/ip2location/ip2location-io-cli/releases/download/v1.1.0/windows.ps1 | iex

Downloading the pre-built binaries

If you just want the executable binaries, you can just download them at the page below and run them.

https://github.com/ip2location/ip2location-io-cli/releases/latest

Installing via Go

If you’re using Debian 12, then you can follow these steps to install Go, otherwise refer to https://go.dev/dl/ for the Go installation steps.

sudo apt update
sudo apt install golang

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

Run the below command to see which path is being used to store the IP2Location.io CLI executable.

go env

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.

export PATH=$PATH:/home/admin/go/bin

After saving the file, you can need to run the below to reload the path.

source ~/.bash_profile

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.

ip2locationio -v

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.

HOW TO USE THE 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.

ip2locationio -h
Display help for IP2Location.io CLI

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.

ip2locationio
Your own public IP geolocation result

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
Pretty print your own public IP geolocation result

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.

ip2locationio 8.8.8.8
IP geolocation result

Or do a pretty print with the below command.

ip2locationio -o pretty 8.8.8.8
Run pretty print command to view the IP result

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 8.8.8.8
Filter the IP geolocation result by fields

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 8.8.8.8
Specify the IP geolocation data by using translations

Conclusion

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.


Was this article helpful?

Related Articles