An IP address is used to uniquely identify a device or a network on the internet. In most cases, it is associated with a specific physical location. It is also important to know that some locations might not be accurate due to the use of VPNs. To locate an IP address you can make use of several free tools available online, command line tools and Geolocation APIs. You can pinpoint the exact location by making use of the longitude and latitude details returned when looking for the location.
IP Geolocation Services
These are websites and online services that provide IP geolocation information. Some of these websites include iplocation.net, whatismyipaddress.com, ipinfo.io, and geoiptool.com among others. Other online services can be found by simply doing a Google search for IP geolocation Services. Most of these services simply require you to write down or paste the IP address and the service will return the location if found.
Command Line Tools
You can use command-line tools like tracert (Windows) or traceroute (Linux/macOS) to trace the route to an IP address. This will provide information about the path taken to get to the IP address
Usage:
tracert <IP address>
traceroute <IP address>
Output:
traceroute to example.com (93.184.216.34), 30 hops max, 60 byte packets
1 router1 (192.168.1.1) 1.245 ms 1.232 ms 1.123 ms
2 ISP-Gateway (203.0.113.1) 8.567 ms 7.890 ms 9.112 ms
3 isp-router (198.51.100.1) 15.678 ms 16.543 ms 14.890 ms
4 core-router (203.0.113.10) 22.567 ms 21.890 ms 23.112 ms
5 border-router (192.0.2.1) 30.567 ms 31.123 ms 29.890 ms
6 example.com (93.184.216.34) 36.112 ms 35.890 ms 37.123 ms
The output will show the different nodes that the data takes to get to the IP Address in question which gives an idea of where the IP Address might be located.
IP Geolocations APIs
You can make use of IP geolocation APIS to get more accurate information. Services like MaxMind, ipstack or OpenCage provide APIs that allow you to integrate IP geolocation in applications or programs. Ensure that you review the documentation and the terms of services to understand the limitations and compliance requirements.
Example:
import requests
ip_address = "8.8.8.8"
api_key = "YOUR_API_KEY" # Get an API key from a provider
url = f"http://api.ipstack.com/{ip_address}?access_key={api_key}"
response = requests.get(url)
data = response.json()
print("City:", data.get("city"))
print("Region:", data.get("region_name"))
print("Country:", data.get("country_name"))
Here is another example using an API key from iponfo.io in a bash command. To get the API key you can visit the ipinfo.io website.This returns the location formation including the coordinates, city and time zone the IP Address is in. The example shows the location information for a google dns server 8.8.8.8
Usage
curl -H "Authorization: Bearer ipinfoAPIkey" https://ipinfo.io/IPAddress
Output
{
"ip": "8.8.8.8",
"hostname": "dns.google",
"anycast": true,
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.4056,-122.0775",
"org": "AS15169 Google LLC",
"postal": "94043",
"timezone": "America/Los_Angeles"
}