How to Get Location Information from an IP Address

Shutterstock / Artistdesign29

This is no CSI magic: it’s easy to get location information from an IP address. The Internet is divided into pieces, called subnets, spread around the world. Getting a location is as easy as finding which subnet an IP address belongs to.

How does it work, anyway?

Subnets are defined by CIDR rating, which is just a concise way of representing a range of IP addresses. For example, 192.168.1.0/24 represents the range from 192.168.1.0 to 192.168.1.255. The number following the slash indicates the number of bits used for the address (in this case the first 24 bits, which make up the first 3 bytes) and the rest is assigned to devices (in this case the last 8 bits , constituting the last number of the address).

Larger chunks are allocated by the The authority for assigning internet numbers (IANA). IANA is responsible for assigning the first number of an IP address, from 0.XXX.XXX.XXX to 255.XXX.XXX.XXX. The allocation of addresses in these blocks is given to more local agencies.

For example, the IP address 173.79.255.255 is part of the block 173.0.0.0/8, which was given to the U.S. Internet Number Registry (ARIN) in 2008. In this block, 173.64 to 173.79 were assigned to Verizon for use in the Northern Virginia region, where our example IP address is located.

You won’t be able to follow a person at home like in the movies, but you can easily get information about the city / region. All of this is public and online, and you can verify your own address using ARIN research tools.

So the only real thing you need to do to get location information from an IP address is to have an array of all the different allocations that IANA, ARIN, and others have made; then you can just search the table like any other database.

The information is all publicly available, so if you want to research it yourself on ARIN website, you can, but there are people who have compiled IP-Location databases before and made the process much easier.

Consult an IP location database

There are a lot of them online, many of them completely free. KeyCDN performs a free search tool, with an API from which you can access it. Just type in the IP address and it will show you all the information it has.

Accessing this information from an API is particularly useful when you want to find a location programmatically. For example, if you want to check the location of problematic IP addresses on your server, you can retrieve the IP address from the log files and run it through an API. If it is foreign or comes from a strange place, it can be malicious.

You can access KeyCDN API from the following URL, with a utility like curl:

curl https://tools.keycdn.com/geo.json?host=173.79.254.254

Or from a programming language like PHP:

$ IP = ‘173.79.254.254’;
$ json = file_get_contents (‘https://tools.keycdn.com/geo.json?host=’. $ IP);
$ obj = json_decode ($ json);

This particular API returns a JSON object with the information. For example, you can find the postcode of an address with response.data.geo.postal_code:

{
“status”: “success”,
“description”: “Data received successfully.”,
“Data”: {
“geo”: {
“host”: “173.79.254.254”,
“ip”: “173.79.254.254”,
“rdns”: “pool-173-79-254-254.washdc.fios.verizon.net”,
“asn”: 701,
“isp”: “MCI Communications Services, Inc. d / b / a Verizon Business”,
“country_name”: “United States”,
“country_code”: “US”,
“region_name”: “Virginia”,
“region_code”: “VA”,
“city”: “Alexandria”,
“postal_code”: “22309”,
“continent_name”: “North America”,
“continent_code”: “NA”,
“latitude”: 38.719,
“longitude”: -77.1067,
“metro_code”: 511,
“timezone”: “America / New_York”,
“datetime”: “2019-08-22 17:30:48”
}
}
}

API throughput is limited to three requests per second, which should be fine for normal use.

If you want to make a large number of requests very quickly, you will need your own database. These are also accessible to the public; IP2Location offers a “Light” version of their database for free, but you can also buy a table with more specific starters. This comes in the form of a CSV, which you can easily load into MySQL or other database solution and access programmatically.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.