由IP地址获得地理信息

一、概述

这篇文章将总结获得一个已知的IP地址的地理信息的各种方法。如何获取IP地址则不在本文讨论范围之内。

二、WEB服务

有很多网站提供这样的服务,国内的有http://www.ip138.com/https://www.v2ex.com/ip,国外的有https://www.maxmind.com/http://ipinfo.io/,当然直接百度某个IP也可以获得其简略的地理信息。下表给出了以上四家网站查询到的信息的具体内容:

URL 主机名(Hostname) 网络类别(Network) 国家(Country) 城市(City) 经纬度(Latitude/Longitude)
http://www.ip138.com/ N Y Y Y N
https://www.v2ex.com/ip N N Y Y Y
https://www.maxmind.com/ N Y Y Y Y
http://ipinfo.io/ Y Y Y Y Y

当然有的网站还提供更多信息,只是没有被列出来,留待读者自己去探索。

通过域名就可以看出,其中最专业的应该是http://ipinfo.io/。这家网站还提供很方便的API,以下内容摘自它的官网:

在Linux终端中执行:

    curl ipinfo.io

返回自己IP地址的相关信息:

    {
      "ip": "211.69.194.67",
      "hostname": "No Hostname",
      "city": "Wuhan",
      "region": "Hubei",
      "country": "CN",
      "loc": "30.5801,114.2734",
      "org": "AS4538 China Education and Research Network Center"
    }

作为参数的IP地址是写在 / 后的:

    curl ipinfo.io/8.8.8.8

返回 / 后IP地址的相关信息:

    {
      "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"
    }

在IP之后再加 / ,之后跟一个键值:

    curl ipinfo.io/8.8.8.8/org

可以方便的提取返回结果中的某一条:

    AS15169 Google Inc.

这个API是免费的,限制是每天最高1,000次请求。当然也有收费版本,详见其官网。

三、命令行工具geoiplookup

Ubuntu下使用如下命令安装:

    sudo apt-get install geoip-bin

在终端执行:

    geoiplookup 8.8.8.8

可以看到输出是:

    GeoIP Country Edition: US, United States

信息比较少,这是因为安装这个工具时同时下载的数据库GeoIP.dat(位于/usr/share/GeoIP)只包含了这些信息。
可以下载更多IP数据库,使geoiplookup的查询结果包含更多信息。
一个常用的下载免费IP数据库的地方是http://dev.maxmind.com/zh-hans/geoip/legacy/geolite/
更简单些,可以通过下面几条命令,用wget下载,解压后直接复制到位:

    wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
    wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
    wget http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
    gunzip GeoIP.dat.gz
    gunzip GeoIPASNum.dat.gz
    gunzip GeoLiteCity.dat.gz
    sudo cp GeoIP.dat GeoIPASNum.dat GeoLiteCity.dat /usr/share/GeoIP/

GeoLite数据库在每个月的第一个星期二更新,所以上面几条命令也应该被每月执行一次。
下载好更多数据库后再来执行:

    geoiplookup 8.8.8.8

可以看到输出是:

    GeoIP Country Edition: US, United States
    GeoIP ASNum Edition: AS15169 Google Inc.

只比以前多了自治系统编号(AS Number)信息,这是因为没有添加额外参数时,geoiplookup将只使用GeoIP.dat和GeoIPASNum.dat。
添加-f参数来指定数据库文件:

    geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat 8.8.8.8

使用了GeoLiteCity.dat数据库,输出信息也是城市一级的:

    GeoIP City Edition, Rev 1: US, CA, California, Mountain View, 94040, 37.384499, -122.088097, 807, 650

四、其他

MaxMind(成立于2002年的一家公司,自称是领先业界的IP智能与在线欺诈检测工具供应商)提供了各个编程语言的API来查询IP地址的地理信息,详见http://dev.maxmind.com/zh-hans/geoip/geoip2/downloadable/#MaxMind_APIs

580scan,自我介绍是:全世界IP查询,IP地址查询,各地IP地址详细信息,IP地址段查询,地区IP地址查询;根据IP地址查询IP的详细地址;根据地区地址查询IP地址段。

(2017.03.10更新)高精度IP定位,这是目前为止我所知的精度最高的定位。参考:互联网定位技术小谈

2 Replies to “由IP地址获得地理信息”

  1. Hi,

    We’re wondering if you’ve ever considered taking the content from werner.wiki and converting it into videos to promote on Youtube? You simply add the text and it converts it into scenes that make up a full video. No special skills are needed, and there’s access to over 1 million images/clips that can be used.

    You can read more about the software here: https://www.vidnami.com/c/Jef2020-vn-freetrial

    Kind Regards,
    Trevor

回复 Werner 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

18 − 1 =