【问题标题】:Use whois to get country from IP address in Perl在 Perl 中使用 whois 从 IP 地址获取国家/地区
【发布时间】:2015-03-30 19:15:44
【问题描述】:

如何在 Perl 中从 IP 地址获取国家/地区?我必须使用 whois 来做到这一点。 我知道,我可以使用国家/地区:

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

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

【问题讨论】:

  • 您看到的国家不一定是“来自 IP 地址的国家”。您应该改为查看 search.cpan.org/~maxmind/Geo-IP-1.45/lib/Geo/IP.pm 之类的内容。
  • 但我必须使用 whois :P 在学校我的任务是使用“whois”并从中获取国家/地区。
  • 不要逃到 shell 去这样做,Perl 中有很多 whois 库。

标签: perl ip country whois


【解决方案1】:
my $country = `whois $ip | grep -Po '^Country:\s*\K.*';
chomp($country);

但是看到-P中的“P”代表“Perl”,我们不妨去掉grep。

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

【讨论】:

    【解决方案2】:

    我用小菜鸟的方式做了它,但它有效xd

    $test = `whois $_ |grep -i country`;
    $rid = rindex($test, ":");
    $b = substr("$test, $rid+1);
    

    现在我只有“DE”、“BR”、“CN”、“MX”等 :)

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 2017-01-28
      • 2013-11-05
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 2022-06-25
      • 1970-01-01
      相关资源
      最近更新 更多