#!/usr/bin/perl 

# Have I seen the ip before
%seen=();
@unique_ip=();

# Directory containing your old log files
my @files=<security_logs> ;

# open each file
foreach $file (@files){
  open (FH,"<$file") || die "Cant open $file:\t$!\n";
  while ( $oneLine = <FH>){
    chomp ($oneLine);

    # For each line in the file, if a line contains an attackalert, Connect, and an ip,
    # then make a new hash element with the ip as a key,
    # and add 1 to the number of times that ip has hit your site.
    #if ( $oneLine =~ m/attackalert.*Connect.*\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/ig ){  
    if ( $oneLine =~ m/attackalert.*\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/ig ){  
      push ( @unique_ip, $1) unless $seen{$1}++;
    } 	
  }
  close (FH);
}
$number=@unique_ip;
print "Found $number unique IPs\n";

#Sort hash by the values
sub byviolations { $seen{$b}<=>$seen{$a}}

# for each hash element, print the ip, number, and star representation for hits, sorted by hits.
foreach $key ( sort byviolations keys %seen ){
  $stars="*"x$seen{$key};
  # format the data using format and write.
  format STDOUT =
  @<<<<<<<<<<<<<<<   ^<<<<<<      ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  $key,               $seen{$key},  $stars
. 
  write();
  #print "$key => $seen{$key}\t","*"x$seen{$key},"\n";
}

