قالب وردپرس درنا توس
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 parts called subnets that are spread around the world. Getting a location is as easy as finding the subnet an IP falls into.

How does it even work?

Subnets are defined by the CIDR notation, which is only a clear representation of 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 after the slash indicates how many bits are used for the address (in this case the first 24 bits that make up the first 3 bytes) and the rest is assigned to the devices (in this case the last 8) bits that form the last number in the address).

The largest blocks 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. The assignment of addresses in these blocks is done to more local agencies.

For example the IP address 173.79.255.255 is part of 173.0.0.0/8 Block that was transferred to the American Internet Number Register (ARIN) in 2008. 173.64 by 173.79 has been assigned to Verizon for use in northern Virginia, where our sample IP address is.

You won’t be able to track someone to their house like in the movies, but you can get city / area information easily. All of this is public and online, and you can check your own address using ARIN’s search tools.

The only thing you need to do to get location information from an IP address is a table of the various assignments 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 search for it yourself on the ARIN website, you can. However, there are people who have already compiled IP location databases and made the process a lot easier.

Consult an IP location database

There are plenty of these online, many of which are completely free. KeyCDN runs a free lookup tool with an API that you can access it through. Just enter the IP address and you will be shown all the information.

Accessing this information through 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 get the IP address from the log files and run it from an API. If it is strange or comes from a strange place, it can be malicious.

You can access the KeyCDN API with a utility such as the following url 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 will return a JSON object with the information. For example, you could 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 a lot of inquiries very quickly, you need your own database. These are also publicly available; IP2Location offers a free lite version of the database. However, you can also buy a table with more detailed entries. This is done in the form of a CSV, which you can easily load into MySQL or another database solution and access programmatically.


Source link