
savo
User
Sep 27, 2009, 5:47 AM
Post #1 of 5
(320 views)
|
|
A few more problems
|
Can't Post
|
|
I decided to carry on my hash that i was playing with and turn it into a usable script, this through a few more problems up and was wondering if i have gone about it the correct way. I thought at one point to use two arrays one for people already banned and another for people to be banned and print the difference but couldn't find any way to compare two arrays is there a simple way to do this? I decided on deleting hash values as this was the only thing i could think of that worked. I am not sure about the amount of times i had to open and close firewall i think there will be a better way of going about this.
#!/usr/bin/perl use warnings; use strict; use 5.010; my %hash; my @list; my @banned; if ( !open MESSEGES, "/var/log/messages" ) { die "didnt open? ($!)"; } while (<MESSEGES>) { if (/Failed password/) { @list = split; if ( $list[12] =~ /^(\d+\.){3}\d+/ ) { $hash{ $list[12] } += 1; } else { if ( $list[10] =~ /^(\d+\.){3}\d+/ ) { $hash{ $list[10] } += 1; } } } } close MESSEGES; if ( !open FIREWALL, "firewall" ) { die "didnt open? ($!)"; } while (<FIREWALL>) { push @banned, $_ if /(\d+\.){3}\d+/; } chomp @banned; close FIREWALL; for my $key ( sort keys %hash ) { delete $hash{$key} if $hash{$key} < 3; } for (@banned) { delete $hash{$_}; } if ( !open FIREWALL, ">>firewall" ) { die "didnt open? ($!)"; } select FIREWALL; for my $key ( sort keys %hash ) { print "$key\n"; } close FIREWALL
|