Mateusz
Mateusz

Reputation: 331

Use whois to get country from IP address in Perl

how can I get country from IP address in Perl? I have to use whois to do it. I know, that to take country I can use:

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

But it returns me "Country: DE". I need just "DE".

Upvotes: 1

Views: 1809

Answers (2)

ikegami
ikegami

Reputation: 386561

my $country = `whois $ip | grep -Po '^Country:\s*\K.*';
chomp($country);

But seeing as the "P" in -P stands for "Perl", we might as well get rid of grep.

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

Upvotes: 1

Mateusz
Mateusz

Reputation: 331

I made it in little noob way, but it works xd

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

And now I have just "DE", "BR", "CN", "MX" etc :)

Upvotes: 0

Related Questions