9

How can I find a location such as city/state or country from an IP address?

Is there a Ruby gem or Python module to do this? Or, is there a website which has an API?

10

You could use the http://ipinfo.io API for this:

$ curl ipinfo.io/8.8.8.8
{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.3860,-122.0838",
  "org": "AS15169 Google Inc.",
  "postal": "94040"
}

If you're only interested in one field you can add that to the URL to get it as plain text:

$ curl ipinfo.io/8.8.8.8/loc
37.3860,-122.0838

$ curl ipinfo.io/8.8.8.8/country
US

If you're looking for a python module https://github.com/juanpabloaj/pyipinfo is a simple wrapper, or http://geocoder.readthedocs.org/en/stable/ is more fully featured and has built in support for ipinfo.io and other providers.

2

Yes, you can find the city, state, or country location of an IP address by using an IP geolocation API. I know of several websites offering such services to companies or curious individuals like you and me.

However, whatever’s available out there would have different features so you need to check them carefully before deciding on the software that would suit your requirements. With regards to the programming languages you mentioned, normally the best IP geolocation APIs support multiple programming languages including Ruby and Python.

You also specified details such as city, state, or country. Well, no matter which API you choose, those geo-data points would definitely be topmost in the output results whenever you make a query. But IP geolocation APIs usually provide more details such as time zone and postal code which are important if you wish to create a clearer picture of the IP address you are geolocating.

Here are the APIs that I personally use.

https://geoipify.whoisxmlapi.com/

This API helps find the geolocation of an IP address (check sample output here) from more than 9.5 million IP blocks in 250 countries. In my opinion, that number should be enough to include almost all of the known IP addresses now in use.

This provider also has a geolocation database that contains both IPv4 and IPv6 addresses with more than 340,000 locations in 120,000+ towns and cities worldwide. What I like is that it’s updated weekly and available for download in CSV or JSON format.

Before making a request, you will need a personal API key which you can get from the website’s ‘My Products’ page. To make a location search, you have the option to input either the IPv4 or IPv6 address, the domain name, or email address of your target.

The search results will contain your target’s country, region, city, latitude, longitude, postal code, time zone, ISP, the array of domains, autonomous system number, system name, API system route, and autonomous system website’s URL.

They are displayed in the default JSON format, although you have the choice to have it in either JSON or XML. System compatibility should not be a problem because the output supports 9 programming languages, including Ruby and Python.

In case you need to make multiple geolocation requests, there’s also an online bulk IP geolocation lookup service which claims to allow you to check up to 100,000 IP addresses in one go.

https:// https://geo.ipify.org/

The second provider I use claims to offer real-time geolocation from more than 8 million IP blocks from 250 countries.

They also have an IP geolocation database that contains almost 5 million accumulated records from almost all of the existing IP addresses (check sample output here). They update it monthly, and it is available for download in a relatively small file size of 40 Mb zipped.

Making a search request requires a personal API key which you can get from this provider’s subscription page. To make a location search, you need to input your target’s IPv4 address.

The query result shows the target’s precise location details including country, region, city, latitude, longitude, postal code, and time zone. The output can be displayed in either XML or JSON format.

The API also supports 9 programming languages — i.e., Javascript, C#, Java, PHP, NodeJS, Perl, PowerShell, as well as Python and Ruby.

Finding the city, state, or country location of an IP address is easy if you use an IP geolocation API. However, remember that your choice must be able to provide ample coverage and programming language compatibility to be able to give you what you are searching for. Geoipify.whoisxmlapi.com and Geo.Ipfy can definitely help in that regard.

1

You can go here...Any such service is only a rough estimate for ANY IP address, but this is what you are looking for.

http://www.geobytes.com/iplocator.htm (just enter the "IP Address to locate" and hit submit)

1
  • 1
    I have a long list of ip addresses so I was hoping I would be able to do it through a script
    – ed1t
    Jun 18 '11 at 18:45
0

If you want to see a geolocation from a website you are navigating easily, you should install Flagfox add-on for Firefox: you will se a flag near address bar and clicking, on it, more information (city, IP...).

Also, you can use http://geo.flagfox.net to see the same detais for any IP address or server name.

0

Hostip.info is an open-source project with the goal to build/maintain a database mapping IP addresses to cities. Their about page explains the data sources relied on to populate this database.

Using HostIP, there are two ways to get location data from an IP address:

They also have a well-designed and easy-to-use RESTFUL API: just pass in your ip address after the ip= in the GET request string):

import urllib

response = urllib.urlopen('http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true').read()

print(response)

Second, the Project Website also makes its complete database available for download.

0

you may use geoiplookup from GeoIP package

geoiplookup -i 88.157.160.26

GeoIP Country Edition: PT, Portugal
ipaddr: 88.157.160.26
range_by_ip:  88.157.0.0 - 88.157.255.255
network:      88.157.0.0 - 88.157.255.255 ::16
ipnum: 1486725146
range_by_num: 1486684160 - 1486749695
network num:  1486684160 - 1486749695 ::16
GeoIP City Edition, Rev 1: PT, 14, Lisboa, Lisbon, 1350-008, 38.716702, -9.133300, 0, 0
ipaddr: 88.157.160.26
range_by_ip:  88.157.160.0 - 88.157.162.255
network:      88.157.160.0 - 88.157.161.255 ::23
ipnum: 1486725146
range_by_num: 1486725120 - 1486725887
network num:  1486725120 - 1486725631 ::23
GeoIP ASNum Edition: AS2860 Nos Comunicacoes, S.A.
ipaddr: 88.157.160.26
range_by_ip:  88.157.0.0 - 88.157.203.255
network:      88.157.128.0 - 88.157.191.255 ::18
ipnum: 1486725146
range_by_num: 1486684160 - 1486736383
network num:  1486716928 - 1486733311 ::18

rpm -qa | grep -iP "geoip|geolite"

GeoIP-GeoLite-data-extra-2018.06-4.el7.noarch
GeoIP-GeoLite-data-2018.06-4.el7.noarch
geolite2-country-20190806-1.el7.noarch
GeoIP-1.6.12-6.0.cf.rhel7.x86_64
geoipupdate-2.5.0-1.el7.x86_64
geolite2-city-20190806-1.el7.noarch
0

Verify proxy exit location without installing anything

I needed this to confirm tor reads updated exit node setting.

curl ipinfo.io/"$(curl -s --proxy socks5://localhost:9050 ifconfig.me)"

Had to do two requests because the limit at ipinfo.io was exceeded for this exit node. Looks like a handy alias, if you think about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.