Sometimes you need to get the geolocation information for an IP address quickly. In Linux, you can use the power of console apps to save your time and retrieve this information quickly. Let's see how it can be done.
To obtain the geo location information about a specific IP address, you need to use some online service which provides the appropriate API. It is a good idea to use some public service to avoid the authorization procedure and the API key management. One such service is FreeGeoIP.net.
It provides a public HTTP API to search the geolocation of IP addresses. It uses a database of IP addresses that are associated with cities along with other relevant information like time zone, latitude and longitude. This is very useful.
The service can provide search results as JSON or XML. So, if we combine curl with some JSON parser, we can get the required info.
I will use my favorite JSON parser, jq:
For our case, the query should be as follows:
http://freegeoip.net/json/119.94.116.145
The "json" portion here is the desired data format. Besides JSON, it can be XML or CSV.
Let's run the query with curl and see the output:
curl http://freegeoip.net/json/119.94.116.145|jq
The output will be easier to read:
Using jq, you can filter the output and make it show only the required fields. The following command will display only the country name, latitude and longitude:
curl http://freegeoip.net/json/119.94.116.145|jq -r '.country_name,.latitude,.longitude'
#!/bin/sh curl -s http://freegeoip.net/json/$1|jq -r '.country_name,.latitude,.longitude'
The next time you need to get geolocation information, you can execute your script like this:
./geo.sh IP_address
That's it.
Support us
Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:
Please delete this outdated article, freegeoip is down
please delete yourself