在Perl中使用whois从IP地址获取国家/地区

在Perl中使用whois从IP地址获取国家/地区,perl,ip,country,whois,Perl,Ip,Country,Whois,如何在Perl中从IP地址获取国家/地区?我必须用谁来做这件事。 我知道,我可以用这个国家: $test = `whois $ip |grep -i country`; 但它会返回我“Country:DE”。我只需要“DE”我用小noob的方式做的,但它工作xd $test = `whois $_ |grep -i country`; $rid = rindex($test, ":"); $b = substr("$test, $rid+1); 现在我只有“DE”、“BR”、“CN”、“MX

如何在Perl中从IP地址获取国家/地区?我必须用谁来做这件事。 我知道,我可以用这个国家:

$test = `whois $ip |grep -i country`;

但它会返回我
“Country:DE”
。我只需要“DE”

我用小noob的方式做的,但它工作xd

$test = `whois $_ |grep -i country`;
$rid = rindex($test, ":");
$b = substr("$test, $rid+1);
现在我只有“DE”、“BR”、“CN”、“MX”等:)

但是鉴于
-p
中的“p”代表“Perl”,我们不妨去掉
grep

my $whois = `whois $ip`;
my ($country) = $whois =~ /^Country:\s*(.*)/m;

您看到的国家不一定是“IP地址国家”。你应该研究类似的东西。但是我不得不在学校里使用whois:P。我的任务是使用“whois”并从中获取国家信息。不要跳转到shell中去做这件事,Perl中有很多whois库。
my $whois = `whois $ip`;
my ($country) = $whois =~ /^Country:\s*(.*)/m;