0
如何从perl中的ip地址获取国家/地区?我必须使用whois来做到这一点。 我知道,拿国家,我可以使用:使用whois从perl中获取IP地址的国家
$test = `whois $ip |grep -i country`;
但它返回我"Country: DE"
。我只需要“DE”。
如何从perl中的ip地址获取国家/地区?我必须使用whois来做到这一点。 我知道,拿国家,我可以使用:使用whois从perl中获取IP地址的国家
$test = `whois $ip |grep -i country`;
但它返回我"Country: DE"
。我只需要“DE”。
我有点小白的方式进行,但它的工作原理XD
$test = `whois $_ |grep -i country`;
$rid = rindex($test, ":");
$b = substr("$test, $rid+1);
而现在我只有 “DE”, “BR”, “CN”, “MX” 等:)
my $country = `whois $ip | grep -Po '^Country:\s*\K.*';
chomp($country);
但是看到-P
代表“Perl”中的“P”,我们不妨去掉grep
。
my $whois = `whois $ip`;
my ($country) = $whois =~ /^Country:\s*(.*)/m;
您所看到的国家不一定是“来自IP地址的国家”。您应该查看http://search.cpan.org/~maxmind/Geo-IP-1.45/lib/Geo/IP.pm之类的内容。 – bzlm 2015-03-30 19:18:43
但我不得不使用whois:P在学校,我的任务是使用“whois”并从中获得国家。 – Mateusz 2015-03-30 19:20:40
不要逃离shell来做到这一点,你有许多在Perl中的whois库。 – 2018-01-02 20:02:18