| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use 5.008; |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
|
| 8 |
use lib 'lib'; |
| 9 |
|
| 10 |
use Data::Dumper; |
| 11 |
use Getopt::Long; |
| 12 |
use GeoIP2::WebService::Client; |
| 13 |
use Try::Tiny; |
| 14 |
|
| 15 |
sub main { |
| 16 |
my %opts = ( service => 'country' ); |
| 17 |
|
| 18 |
GetOptions( |
| 19 |
'user-id:s' => \$opts{account_id}, # for backwards-compatibility |
| 20 |
'account-id:s' => \$opts{account_id}, |
| 21 |
'license-key:s' => \$opts{license_key}, |
| 22 |
'ip-address:s' => \$opts{ip}, |
| 23 |
'service:s' => \$opts{service}, |
| 24 |
'host:s' => \$opts{host}, |
| 25 |
'help' => \$opts{help}, |
| 26 |
); |
| 27 |
|
| 28 |
if ( $opts{help} ) { |
| 29 |
_help(); |
| 30 |
exit 0; |
| 31 |
} |
| 32 |
|
| 33 |
unless ( $opts{account_id} && $opts{license_key} && $opts{ip} ) { |
| 34 |
_help('You must provide your account ID, license key, and IP address.'); |
| 35 |
exit 1; |
| 36 |
} |
| 37 |
|
| 38 |
my $service = delete $opts{service} // 'country'; |
| 39 |
|
| 40 |
unless ( GeoIP2::WebService::Client->can($service) ) { |
| 41 |
_help("The service you asked for ($service) does not exist."); |
| 42 |
exit 1; |
| 43 |
} |
| 44 |
|
| 45 |
my $ip = delete $opts{ip}; |
| 46 |
|
| 47 |
for my $k ( keys %opts ) { |
| 48 |
delete $opts{$k} unless defined $opts{$k}; |
| 49 |
} |
| 50 |
|
| 51 |
my $client = GeoIP2::WebService::Client->new(%opts); |
| 52 |
|
| 53 |
my $model; |
| 54 |
try { |
| 55 |
$model = $client->$service( ip => $ip ); |
| 56 |
} |
| 57 |
catch { |
| 58 |
warn "\n** Got an error when trying to look up the IP '$ip'\n\n"; |
| 59 |
warn ' Message: ', $_->message(), "\n"; |
| 60 |
if ( $_->can('code') ) { |
| 61 |
warn ' Code: ', $_->code(), "\n"; |
| 62 |
} |
| 63 |
if ( $_->can('http_status') ) { |
| 64 |
warn ' HTTP Status: ', $_->http_status(), "\n"; |
| 65 |
} |
| 66 |
|
| 67 |
exit 2; |
| 68 |
}; |
| 69 |
|
| 70 |
{ |
| 71 |
local $Data::Dumper::Terse = 1; |
| 72 |
local $Data::Dumper::Indent = 1; |
| 73 |
local $Data::Dumper::Useqq = 1; |
| 74 |
local $Data::Dumper::Deparse = 1; |
| 75 |
local $Data::Dumper::Quotekeys = 0; |
| 76 |
local $Data::Dumper::Sortkeys = 1; |
| 77 |
print Dumper( $model->raw() ); |
| 78 |
} |
| 79 |
|
| 80 |
exit 0; |
| 81 |
} |
| 82 |
|
| 83 |
sub _help { |
| 84 |
my $error = shift; |
| 85 |
|
| 86 |
print "\n** $error\n\n" if $error; |
| 87 |
|
| 88 |
print <<'EOF'; |
| 89 |
web-service-request --account-id [id] --license-key [key] --ip-address [ip] |
| 90 |
|
| 91 |
This script will query the MaxMind GeoIP web service for information about |
| 92 |
the given IP address. |
| 93 |
|
| 94 |
It accepts the following options: |
| 95 |
|
| 96 |
--account-id Your MaxMind account ID (required) |
| 97 |
--license-key Your MaxMind license key (required) |
| 98 |
--ip-address The IP address to look up (required) |
| 99 |
--service One of country, city, city_isp_org, or omni. Defaults to country. |
| 100 |
--host The host to use. Defaults to geoip.maxmind.com. |
| 101 |
|
| 102 |
EOF |
| 103 |
} |
| 104 |
|
| 105 |
main(); |