قالب وردپرس درنا توس
Home / Tips and Tricks / How to get your system's geographical location from a base program

How to get your system's geographical location from a base program



  World Map with Internet Connection Concept
Toria / Shutterstock.com

You can find the geographical location of a remote Linux system with open API and a simple bash script. Geolocation of a server can help you track it in the physical world so that the servers are in regional hotspots.

Every server on the internet has an IP address that is open to the public. This is assigned either directly to the server or assigned to a router that sends traffic to that server. IP addresses give us a clue as to where that server is in the world. We can get this geolocation data through two open APIs, provided by ipinfo.co and IP Vigilante, using it to see the city, state, and country associated with a server or remote system. This does not give you an exact GPS location that lets you only see the general area of ​​the IP address.

Connect to a remote system

You run the following commands on the Linux server or other remote systems you want to geolocate, so you need to connect to the server and access a shell on it first. For example, you can connect via SSH .

Installing curl and jq

We need two tools to get to the geolocation API: curl to make HTTP requests and jq to process JSON data as we come back. Open a terminal and use apt-get to install these tools on Ubuntu or Debian based systems.

  sudo apt-get install curl jq 

Find the server's public IP address

We also need the server's public IP address before we can get geolocation data. Use curl to make an API call to ipinfo.io in your terminal window.

  curl https://ipinfo.io/ip

Get location data from API

Now that we have the server's public IP, we can call the ipvigilante.com API to get geolocation data. Replace with the address that came back in the previous command.

  curl https://ipvigilante.com/

output from curl command

Let us take a closer look at what data we get back from this call:

 metadata showing location information

The API returns the city, country and continent where our server resides. It also returns approximate latitude and longitude coordinates if we want to drag this server onto an interactive map. We use "latitude", "longitude", "city code" and "country name" in our script. The command understands how to process API data and extracts these four fields.

Create a script to automate the API call

We can create a script that grabs geolocation data and writes it to a file in CSV format. The data will be written to a file named server_location.txt in the directory / tmp / . Open your favorite editor and create a script named geolocate.sh . Insert the script contents shown below and be sure to replace the IP address with your own:

  #! / Bin / sh

Output_file = / tmp / server_location.txt

# Grab this public server's IP address
PUBLIC_IP = `curl -s https: // ipinfo.io / ip`

# Call the geolocation API and capture the output
curl-s https://ipvigilante.com/169.255.X.X | 
jq & # 39; .data.latitude, .data.longitude, .data.city_name, .data.country_name & # 39; | 
while reading -r LATITUDE; do
read -r LONGITUDE
read -r CITY
read -R COUNTRY
echo "$ {LATITUDE}, $ {LONGITUDE}, $ {CITY}, $ {COUNTRY}" | 
tr - delete  t
$ {Output_file}
Done

Save the script and return to the terminal. Make the script executable from the terminal by granting permission for this file.

  chmod u + x geolocate.sh 

Now you're ready to test it. Run the geolocate.sh script and check the contents of the output file:

  ./ geolocate.sh
cat /tmp/server_location.txt

 run the geolocate script

Update Geolocation Data Once a day with a Cron job

Let's create a cron job to make our server update its geolocation and save it to a file once one day. The daily cron job updates a file named server_location.txt in the / tmp / folder on the server. Creating a 24-hour cron job is as easy as putting our script in the directory /etc/cron.daily . We need to use the sudo command to copy the file as root user, to avoid state issues. Run the following command to copy geolocate.sh to the directory /etc/cron.daily .

  sudo cp geolocate.sh /etc/cron.daily

These changes are immediate and our script runs every 24 hours to update the contents of the file /tmp/server_location.txt . We can use these data to make interesting things, such as plotting our servers on a map, and combining geolocation with traffic logs to see where in the world our server hotspots are.


Source link