Return to Snippet

Revision: 4486
at December 21, 2007 07:53 by iblis


Initial Code
#!/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: $!\n";
#
# 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} . "\t" . $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+(.+?)\n/) {
		$descr = $1 ;
	}
	print "\t" . lc($country);
	print "  " . lc($descr) . "\n"; 
}
#
# command line alternative (without dns info):
# awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c

Initial URL

                                

Initial Description

                                

Initial Title
List apache server clients IPs by occurence (with country and provider info)

Initial Tags
apache, log, web

Initial Language
Perl