Posted By

iblis on 12/21/07


Tagged

apache log administration web


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

willcodeforfood


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


 / Published in: Perl
 

  1. #!/usr/bin/perl -w
  2. use strict;
  3. #
  4. # List apache server clients IPs by occurence (with country and provider info)
  5. # perl administration web log apache
  6. #
  7. my %ips;
  8. my $logfile = '/var/log/apache2/access.log';
  9. open my $fh, '<', $logfile
  10. or die "cannot open file $logfile: $!\n";
  11. #
  12. # hash ip occurences
  13. while (<$fh>) {
  14. $ips{$1}++ if (/^(\d+\.\d+\.\d+\.\d+)/);
  15. }
  16. #
  17. # sort ip by occurence and print
  18. # get whois data, parse and print
  19. foreach my $ip (sort { $ips{$b} <=> $ips{$a} } keys %ips) {
  20. print $ips{$ip} . "\t" . $ip;
  21. my $result = qx{ whois -B -h whois.ripe.net $ip };
  22. my ($country, $descr) = ('', '');
  23. if ($result =~ /country:\s+(\w+)/) {
  24. $country = $1;
  25. }
  26. if ($result =~ /descr:\s+(.+?)\n/) {
  27. $descr = $1 ;
  28. }
  29. print "\t" . lc($country);
  30. print " " . lc($descr) . "\n";
  31. }
  32. #
  33. # command line alternative (without dns info):
  34. # awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c

Report this snippet  

You need to login to post a comment.