此配置将为您提供一个简单的perl脚本,可用于从IP地址查找国家名称。首先,我们需要安装libgeo ipfree perl
perl库:
UBUNTU/DEBIAN # apt-get install libgeo-ipfree-perl
创建一个perl脚本 ip2location.pl
,代码如下:
#!/usr/bin/env perl use Geo::IPfree; my $geo = Geo::IPfree->new; my( $code, $country ) = $geo->LookUp( $ARGV[0] ); print "Country: $country\n" . "Country Code: $code\n"
使脚本可执行文件:
$ chmod +x ip2location.pl
我们的ip2location.pl
脚本只接受一个命令行参数,这是我们希望转换/查找为国家名称的IP地址。例如,我们使用参数“213.213.65.125”执行脚本:
./ip2location.pl 213.213.65.125 Country: Italy Country Code: IT
上述脚本可用作一个简单的命令行工具,将IP地址转换为国家/地区位置,也可以用于分析Apacheaccess.log
并将其中的所有IP地址转换为国家/地区位置:
$ for i in $( awk '{ print $1} ' access.log | sort | uniq ); do perl ip2location.pl $i; done
此外,perl的'Geo::IPfree'库还可用于查找主机名:
$ ./ip2location.pl gnu.org Country: United States Contry Code: US
日期:2020-07-07 20:54:38 来源:oir作者:oir