قالب وردپرس درنا توس
Home / Tips and Tricks / How to get location information from an IP address – CloudSavvy IT

How to get location information from an IP address – CloudSavvy IT



Shutterstock / Artistdesign29

It’s not CSI magic ̵

1; it’s easy to get location information from an IP address. The Internet is divided into pieces, so-called subnets, which are spread all over the world. Getting a place is as easy as finding which subnet an IP ends up in.

How does it work anyway?

Subnets are defined by CIDR listing, which is only a concise way of representing a number 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 how many bits are used for the address (in this case the first 24 bits are given, which make up the first 3 bytes) and the rest are given in units (in this case the last 8 bits, which is the last number in the address).

The largest chunks are assigned by the Internet Assigned Numbers Authority (IANA). IANA is responsible for assigning the first number of an IP address, from 0.XXX.XXX.XXX to 255.XXX.XXX.XXX. Assignment of addresses in these blocks is given to more local authorities.

For example, the IP address 173.79.255.255 is a part of 173.0.0.0/8 blocks given to the American Registry for Internet Numbers (ARIN) 2008. Within this block, 173.64 by 173.79 was awarded to Verizon for use in the Northern Virginia area, this is where our example of IP address falls.

You will not be able to trace anyone to their house as they do in the movies, but you can easily get information about the city / region. All this is public and online, and you can check your own address with ARIN’s search tool.

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

The information is available to everyone, so if you want to search for it on ARIN’s website, you can, but there are people who have already compiled IP site databases and made the process much easier.

Consult an IP site database

There are many of these online, with many of them completely free. KeyCDN runs a free search tool, complete with an API that you can access it from. Just enter the IP address and it will show all the information it has.

Having access to this information from an API is especially useful when you want to find a place programmatically. For example, if you want to check the location of problematic IP addresses hitting your server, you can take the IP address from the log files and run it with an API. If it is foreign or comes from a strange place, it can be harmful.

You can access KeyCDN’s API from the following URL, using a tool such as 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 specific API returns a JSON object with the information. For example, you can find the zip code of an address with response.data.geo.postal_code:

{
  "status": "success",
  "description": "Data successfully received.",
  "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"
    }
  }
}

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

If you want to make many requests very quickly, you need your own database. These are also widely available; IP2Location offers a “Lite” version of their database for free, but you can also buy a table with more accurate records. This comes in the form of a CSV, which you can easily upload to MySQL or another database solution and access programmatically.


Source link