Guest User

Untitled

a guest
Apr 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.58 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $access_log = $ARGV[0] || '/var/log/apache2/access.log';
  7. my %user_agents = ();
  8.  
  9. if (open FILE,'<',"$access_log") {
  10.     while (<FILE>) {
  11.         my $line = $_;
  12.         if (my ($ua) = ($line =~ /\"([^"]+)\"$/)) {
  13.             $user_agents{$ua}++;
  14.         }
  15.     }
  16.     close FILE;
  17. } else {
  18.     print STDERR "could not open \'$access_log\': $!\n";
  19.     exit;
  20. }
  21.  
  22. my @sorted = sort { $user_agents{$b} <=> $user_agents{$a} } keys %user_agents;
  23.  
  24. foreach my $user_agent (@sorted) {
  25.     print "$user_agent: $user_agents{$user_agent}\n";
  26. }
Add Comment
Please, Sign In to add comment