-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexample.pl
executable file
·70 lines (58 loc) · 1.95 KB
/
example.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl
#
use strict;
use warnings;
use lib 'Geo-IPinfo/lib';
use Geo::IPinfo;
use Data::Dumper;
use JSON;
my $token = '1234567';
my %custom_countries = (
"US" => "Custom United States",
"DE" => "Custom Germany"
);
my @custom_eu_countries = ( "FR", "DE" );
# if you have a valid token, use it
my $ipinfo = Geo::IPinfo->new($token);
# or, if you don't have a token, use this:
# my $ipinfo = Geo::IPinfo->new();
# provide your own countries and eu countries
# my $ipinfo = Geo::IPinfo->new($token, countries => \%custom_countries, eu_countries => \@custom_eu_countries);
# return a hash reference containing all IP related information
my $data = $ipinfo->info('8.8.8.8');
if ( defined $data ) # valid data returned
{
# use Data::Dumper to see the contents of the hash reference (useful for debugging)
print Dumper($data);
# loop and print key-value paris
print "\nInformation about IP 8.8.8.8:\n";
for my $key ( sort keys %{$data} ) {
printf "%10s : %s\n", $key,
defined $data->{$key} ? $data->{$key} : "N/A";
}
print "\n";
# print JSON string
my $json = JSON->new->allow_blessed->convert_blessed;
my $json_string = $json->utf8->pretty->encode($data);
print $json_string . "\n";
}
else # invalid data obtained, show error message
{
print $ipinfo->error_msg . "\n";
}
# you can also retrieve information for IPv6 addresses in a similar fashion
my $ipv6_data = $ipinfo->info_v6('2001:4860:4860::8888');
if ( defined $ipv6_data ) # valid data returned
{
# print JSON string
my $json = JSON->new->allow_blessed->convert_blessed;
my $json_string = $json->utf8->pretty->encode($ipv6_data);
print $json_string . "\n";
}
else # invalid data obtained, show error message
{
print $ipinfo->error_msg . "\n";
}
# retrieve only city information of the IP address
my $details = $ipinfo->field( '8.8.8.8', 'city' );
print "The city of 8.8.8.8 is " . $details->city . "\n";