#!/usr/bin/perl # Read IP addresses from first field of Apache logfile and determine country use strict; use Net::DNS; my $domain = "qupps.geo"; my $res = Net::DNS::Resolver->new( # create resolver object nameservers => [qw(127.0.0.3)] ); my ($ip, $rev, $query); my %ipcache; # cache identical IP my %countrylist; # count countries while (<>) { chomp; s/\s.*$//; # leaves only IP if (defined($ipcache{$ip = $_})) { $countrylist{$ipcache{$ip}}++; # IP seen; incr.counter next; } my ($a, $b, $c, $d) = split(/\./, $_, 4); $rev = "$d.$c.$b.$a.$domain"; $query = $res->search($rev, 'TXT'); # perform query if ($query) { my $rr = ($query->answer)[0]; if ($rr->type eq 'TXT') { $ipcache{$ip} = $rr->txtdata; $countrylist{$rr->txtdata}++; } } else { warn "query $rev failed: ", $res->errorstring, "\n"; } } foreach my $country (keys %countrylist) { print $countrylist{$country} . " " . $country . "\n"; }