简述:在Linux命令行下查询IP归属地。
对Ubuntu/Debian系统,使用APT命令进行安装:
$ sudo apt-get install geoip-bin
该包由MaxMind提供,它同时提供了国家、城市和自治系统号(ASN)查询。
自治系统号(ASN)是什么? ASN - Autonomous System Number:自治系统号。
AS - Autonomous System:自治系统。 自治系统是指在互联网中,有权自主决定本地网络系统采取何种路由协议的小型单位。 这里所称的本地网络系统可以是一个单一局域网,也可以是多个局域网集群,例如一家企业、一所大学。一个自治系统会分配一个全局的唯一的识别号,这个号码称之为自治系统号。 自治系统内部使用内部网关协议进行路由信息的通信,自治系统之间使用边界网关协议进行路由信息的通信。 一个自治系统也可以视为一个或多个网络运营商来运行一个或多个网络协议前缀的网络连接组合,这些运营商往往都具有单独的明确的路由策略。
这个包完成安装后,会自动安装两个免费的国家IP数据库,默认安装地址为:/usr/share/GeoIP/GeoIP.dat, /usr/share/GeoIP/GeoIPv6.dat,这个数据库在安装时不一定是最新的,你需要自己手动更新,大概每月更新一次。下载地址为:GeoLite Free Downloadable Databases,提供的下载有国家、城市、ASN三类数据库,可以全部使用以下方法下载到 /usr/share/GeoIP 目录下。
$ cd /usr/share/GeoIP
$ sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz -O GeoIP.dat.gz
$ sudo gzip -d GeoIP.dat.gz
查询IP所属国家(第三行 GeoIP ASNum需要下载了GeoIPASNum.dat):
$ geoiplookup 8.8.8.8
GeoIP Country Edition: US, United States
GeoIP ASNum Edition: AS15169 Google Inc.
查询IP所属城市(需要下载GeoLiteCity.dat):
$ geoiplookup 8.8.8.8 -f GeoLiteCity.dat
GeoIP City Edition, Rev : US, N/A, N/A, N/A, 38.000000, -97.000000, , 0 $ geoiplookup 163.177.65.160 -f GeoLiteCity.dat
GeoIP City Edition, Rev : CN, , Guangzhou, N/A, 23.116699, 113.250000, , 0 $ geoiplookup 125.39.240.113 -f GeoLiteCity.dat
GeoIP City Edition, Rev : CN, , Tianjin, N/A, 39.142200, 117.176697, ,
对应字段:
GeoIP City Edition, Rev : country, region, city, postalCode, latitude, longitude, metroCode, areaCode GeoIP City Edition, Rev : CN, , Tianjin, N/A, 39.142200, 117.176697, ,
查询当前数据库版本:
$ geoiplookup 125.39.240.113 -v
GeoIP Country Edition: GEO-106FREE Build Copyright (c) MaxMind Inc All Rights Reserved
GeoIP ASNum Edition: GEO- Build Copyright (c) MaxMind Inc All Rights Reserved
GeoIP Country V6 Edition: GEO-106FREE Build Copyright (c) MaxMind Inc All Rights Reserved
GeoIP Large Country Edition: GEO-106FREE Build Copyright (c) MaxMind Inc All Rights Reserved
GeoIP Large Country V6 Edition: GEO-106FREE Build Copyright (c) MaxMind Inc All Rights Reserved
GeoIP ASNum V6 Edition: GEO- Build Copyright (c) MaxMind Inc All Rights Reserved
查看更多信息:
$ geoiplookup 125.39.240.113 -i
GeoIP Country Edition: CN, China
ipaddr: 125.39.240.113
range_by_ip: 125.31.192.0 - 125.47.255.255
network: 125.32.0.0 - 125.47.255.255 ::
ipnum:
range_by_num: -
network num: - ::
GeoIP ASNum Edition: AS4837 CNCGROUP China169 Backbone
ipaddr: 125.39.240.113
range_by_ip: 125.35.128.0 - 125.46.111.255
network: 125.36.0.0 - 125.39.255.255 ::
ipnum:
range_by_num: -
network num: - ::
MaxMind定期测试IP归属地查询准确性,其官网提供了针对不同国家的测试结果:
Country Correctly resolved within forty kilometers Incorrectly resolved City is unknown
China % % %