一馬力のメモ帳

MVSからLinuxまで。基盤屋さんは眠らない

このIP何処の国のグローバルIPかなぁ」って時。GeoIPも便利

「このIP何処の国のグローバルIPかなぁ」って調べるとき
whois(https://ja.wikipedia.org/wiki/WHOIS)で調べるのが基本。
でも,いつでもインターネットに繋がって,whoisを確認できるわけでもない!って時に役に立つのがGeoIP

GeoIPはIPと国や都市を紐付けるライブラリを含む。
公開しているMaxMind社に感謝して早速インストールしてみるメモ

目次


GeoIPのインストール

GeoIPはRPMの形で配布されているので,CentOSならyum installでインストールできる。

GeoIPパッケージのインストール

まずはパッケージを"yum install GeoIP"コマンドでインストール

[root@CentOS7 ~]# yum install GeoIP
読み込んだプラグイン:fastestmirror, priorities
Loading mirror speeds from cached hostfile
 * elrepo: ftp.ne.jp
 * epel: ftp.iij.ad.jp
 * remi-safe: mirror.innosol.asia
 * rpmforge: ftp.kddilabs.jp
151 packages excluded due to repository priority protections
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ GeoIP.x86_64 0:1.5.0-9.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

================================================================================
 Package         アーキテクチャー バージョン               リポジトリー    容量
================================================================================
インストール中:
 GeoIP           x86_64           1.5.0-9.el7              base           709 k

トランザクションの要約
================================================================================
インストール  1 パッケージ

総ダウンロード容量: 709 k
インストール容量: 1.6 M
Is this ok [y/d/N]: y
Downloading packages:
GeoIP-1.5.0-9.el7.x86_64.rpm                                                                                                                     | 709 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : GeoIP-1.5.0-9.el7.x86_64                                                                                                                1/1
  検証中                  : GeoIP-1.5.0-9.el7.x86_64                                                                                                                1/1

インストール:
  GeoIP.x86_64 0:1.5.0-9.el7

完了しました!
[root@CentOS7 ~]#

GeoIPデータベースのアップデート

GeoIPパッケージをインストールすると,"/usr/share/GeoIP/"配下に
Geo〜.dateファイルが保存されている。
このままでも利用可能だが,データを最新化した方がより正確なデータを入手できる。
"geoipupdate"コマンドを利用することでアップデート可能

  • GeoIP-initial.dat # 初期導入時に作成されるモジュール
  • GeoLiteCity.dat # (更新対象)都市別
  • GeoLiteCountry.dat # (更新対象)国別
[root@CentOS7 ~]# geoipupdate -v
Opened License file /etc/GeoIP.conf
Read in license key 000000000000
number of product ids 2
Connecting to MaxMind GeoIP server
via Host or Proxy Server: api.maxmind.com:80
sending request GET /app/update_getfilename?product_id=506 HTTP/1.0
Host: updates.maxmind.com

database product id 506 database file name /usr/share/GeoIP/GeoLiteCountry.dat
/usr/share/GeoIP/GeoLiteCountry.dat can't be opened, proceeding to download database
MD5 sum of database /usr/share/GeoIP/GeoLiteCountry.dat is 0000000000000000000000000000000
Connecting to MaxMind GeoIP Update server
sending request GET /app/update_getipaddr HTTP/1.0
Host: updates.maxmind.com
client ip address: xxx.xxx.xxx.xxx
md5sum of ip address and license key is 867b31782d99396c3ae8e4282a52dc4d
sending request GET /app/update_secure?db_md5=0000000000000000000000000000000&challenge_md5=867b31782d99396c3ae8e4282a52dc4d&user_id=999999&edition_id=506 HTTP/1.0
Host: updates.maxmind.com

Downloading gzipped GeoIP Database...
Done
Updating /usr/share/GeoIP/GeoLiteCountry.dat
Saving gzip file to /usr/share/GeoIP/GeoLiteCountry.dat.gz ... download data to a gz file named /usr/share/GeoIP/GeoLiteCountry.dat.gz
Done
Uncompressing gzip file ... Done
Performing sanity checks ... Database type is 1
database_info  PASS  lookup  testing GEOIP_COUNTRY_EDITION
PASS
Done
Updated database
Connecting to MaxMind GeoIP server
via Host or Proxy Server: api.maxmind.com:80
sending request GET /app/update_getfilename?product_id=533 HTTP/1.0
Host: updates.maxmind.com

database product id 533 database file name /usr/share/GeoIP/GeoLiteCity.dat
/usr/share/GeoIP/GeoLiteCity.dat can't be opened, proceeding to download database
MD5 sum of database /usr/share/GeoIP/GeoLiteCity.dat is 0000000000000000000000000000000
md5sum of ip address and license key is 867b31782d99396c3ae8e4282a52dc4d
sending request GET /app/update_secure?db_md5=0000000000000000000000000000000&challenge_md5=867b31782d99396c3ae8e4282a52dc4d&user_id=999999&edition_id=533 HTTP/1.0
Host: updates.maxmind.com

Downloading gzipped GeoIP Database...
Done
Updating /usr/share/GeoIP/GeoLiteCity.dat
Saving gzip file to /usr/share/GeoIP/GeoLiteCity.dat.gz ... download data to a gz file named /usr/share/GeoIP/GeoLiteCity.dat.gz
Done
Uncompressing gzip file ... Done
Performing sanity checks ... Database type is 2
database_info  PASS  lookup  testing GEOIP_CITY_EDITION
PASS
Done
Updated database
[root@CentOS7 ~]#

GeoIPの利用方法

GeoIPをコマンドラインで利用する場合は"geoiplookup"コマンドを使う
google DSN のグローバルIP "8.8.8.8"を例に色々な使い方をしてみる

IPから国名を検索

[root@CentOS7 GeoIP]# geoiplookup  8.8.8.8
GeoIP Country Edition: US, United States
[root@CentOS7 GeoIP]#

IPから詳細情報を検索

[root@CentOS7 GeoIP]# geoiplookup  -i 8.8.8.8
GeoIP Country Edition: US, United States
  ipaddr: 8.8.8.8
  range_by_ip:  8.7.245.0 - 8.10.6.242
  network:      8.8.0.0 - 8.9.255.255 ::15
  ipnum: 134744072
  range_by_num: 134739200 - 134874866
  network num:  134742016 - 134873087 ::15

IPから詳細情報を検索(都市名)

[root@CentOS7 GeoIP]# geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat 8.8.8.8
GeoIP City Edition, Rev 1: US, CA, California, Mountain View, 94040, 37.384499, -122.088097, 807, 650

参考資料

Linux】geoiplookupコマンド
http://ameblo.jp/swdis/entry-11590131944.html

IPアドレスの地理的位置情報が引ける無料RESTful API
http://tokkono.cute.coocan.jp/blog/slow/index.php/web-technology/ip-geolocation-restful-apis/

IPアドレスから、発信元の国を検索 - GeoIP など
http://blog.livedoor.jp/altair8800/archives/51159339.html

http://geoiplookup.net

試した環境

[root@CentOS7 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@CentOS7 ~]# uname -a
Linux CentOS7 3.10.0-327.10.1.el7.x86_64 #1 SMP Tue Feb 16 17:03:50 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@CentOS7 ~]# rpm -qa|grep GeoIP
GeoIP.x86_64 0:1.5.0-9.el7