用于在auth.log中对IP地址进行地理定位的脚本

有没有一种方法来自动地定位Ubuntu的Linux上的IP地址? 我正在寻找这样做的auth.log中的错误。

Ubuntu PreReqs:
sudo apt-get install libgeoip1 libgeo-ip-perl libregexp-common-perl

我的脚本只为你:

 #Parses out ip and prints ip followed by country use strict; use warnings; use Regexp::Common qw /net/; use Geo::IP; my $gi = Geo::IP->new(GEOIP_STANDARD); while (<>) { #Following matches IPv4 addresses and stores the result in $1 #The way this is now, it will only do the first IP on each line if (/($RE{net}{IPv4})/g) { print $1 . ':' . $gi->country_code_by_addr($1); } } 

input输出:

 65.19.146.2 65.19.146.2:US 65.19.146.2 220.248.0.0:CN 

脚本只是循环它的input,所以如果脚本被称为foo.pl并且是可执行的,你可以做一些像cat access.log | foo.pl cat access.log | foo.pl 如果您想要更准确的细节,请参阅Geo :: IP perl模块文档(您可能需要安装不同的数据库)。

在Perl中它应该相当简单。 只要取得auth.log并从grep或awk中获取IP列表,然后将IP列表input到Perl脚本中,然后使用Geo :: IP从中获得国家/城市匹配。

从命令行 :

 GeoipLookUp(){ curl -A "Mozilla/5.0" -s "http://www.geody.com/geoip.php?ip=$1" | grep "^IP.*$1" | html2text; }