#!/usr/bin/perl -w
use strict;
#
# List apache server clients IPs by occurence (with country and provider info)
# perl administration web log apache
#
my %ips;
my $logfile = '/var/log/apache2/access.log';
open my $fh, '<', $logfile
or die "cannot open file $logfile: $!
";
#
# hash ip occurences
while (<$fh>) {
$ips{$1}++ if (/^(d+.d+.d+.d+)/);
}
#
# sort ip by occurence and print
# get whois data, parse and print
foreach my $ip (sort { $ips{$b} <=> $ips{$a} } keys %ips) {
print $ips{$ip} . " " . $ip;
my $result = qx{ whois -B -h whois.ripe.net $ip };
my ($country, $descr) = ('', '');
if ($result =~ /country:s+(w+)/) {
$country = $1;
}
if ($result =~ /descr:s+(.+?)
/) {
$descr = $1 ;
}
print " " . lc($country);
print " " . lc($descr) . "
";
}
#
# command line alternative (without dns info):
# awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c
List apache server clients IPs by occurence (with country and provider info)
Leave a Reply
You must be logged in to post a comment.