We Recommend
Learning Perl
In this smooth, carefully paced course, a leading Perl trainer teaches you to program in the language that threatens to make C, sed, awk, and the Unix shell obsolete for many tasks. This book is the "official" guide for both formal (classroom) and informal learning. It is fully accessible to the novice programmer.
Posted By
iblis on 12/21/07
Tagged
apache log administration web
Versions (? )
12/21/07 07:53am
Who likes this? 1 person has marked this snippet as a favorite
willcodeforfood
List apache server clients IPs by occurence (with country and provider info)
Published in: Perl
#!/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 ;
}
}
#
# command line alternative (without dns info):
# awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c
Report this snippet