This article will assist you to find how to get the IP address geographic location of a remote Linux system using open APIs and a simple bash script from the command line. Each server has a public-facing IP address on the internet, which is assigned directly to the server or via a router that transmits network traffic to that server.

 

IP addresses provide a simple way to track the location of the server in the world by using two useful APIs provided by ipinfo.io and ipvigilante.com to get the city, state, and country connected with a server.

 

Install Curl and jq

 

For getting the IP address geographic location of the server, It is required to install the curl command line downloader and jq command-line tool to process the JSON data from the geolocation APIs.

$ sudo apt install curl jq #Ubuntu/Debian
$ sudo yum install curl jq #CentOS/RHEL
$ sudo dnf install curl jq #Fedora 22+
$ sudo zypper install curl jq #openSUSE

 

 





Find the Server’s Public IP Address

For getting the server’s public IP address, apply the following curl command to make an API request to ipinfo.io in your terminal as shown.

$ curl https://ipinfo.io/ip

 

 


Get IP Location Data From The API

Once you have got the server's public IP address, you can now request ipvigilante.com‘s API to fetch the geolocation data using the following command. Make sure to replace <your IP address> with the server’s public IP

$ curl https://ipvigilante.com/<your ip address>

 



 

This is the data we get from the above command.

 

Automate API Call using Bash Script

Now to automate the API process, we will create a script called getipgeoloc.sh (you can name it anything you want) using any of your favorite command-line editors.

$ vim getipgeoloc.sh

 


Then copy and paste the following long command in it.

curl -s https://ipvigilante.com/$(curl -s https://ipinfo.io/ip) | jq '.data.latitude, .data.longitude, .data.city_name, .data.country_name'

 


Save the file and make the script executable with the following command.

">$ chmod +x getipgeoloc.sh

 


Finally, run the script to get your Linux IP geographical location as shown in the following screenshot.

$ ./getipgeoloc.sh

 



The above script shows the city and country name along with the approximate latitude and longitude coordinates.

 

Alternatively, you can also run the above command without saving it in a script as shown.

$ curl -s https://ipvigilante.com/$(curl -s https://ipinfo.io/ip) | jq '.data.latitude, .data.longitude, .data.city_name, .data.country_name'

 

 

 

Was dit antwoord nuttig? 0 gebruikers vonden dit artikel nuttig (0 Stemmen)