CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
A few more problems

 



savo
User

Sep 27, 2009, 5:47 AM

Post #1 of 5 (570 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.


Code
#!/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



savo
User

Sep 27, 2009, 6:59 AM

Post #2 of 5 (564 views)
Re: [savo] A few more problems [In reply to] Can't Post

sorry forgot to add would it be better to use cron to run the script every so often or put in some kind of wait and redo?


shawnhcorey
Enthusiast


Sep 27, 2009, 9:13 AM

Post #3 of 5 (555 views)
Re: [savo] A few more problems [In reply to] Can't Post


In Reply To
sorry forgot to add would it be better to use cron to run the script every so often or put in some kind of wait and redo?


cron

If you write it as a daemon (wait & redo), then there is additional overhead. It has to periodically kill itself off and restart. It has to be able to restart if something else kills it. It has to restart on reboot. It has to assume that any opened file may not have been flushed and therefore is corrupt.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


savo
User

Sep 28, 2009, 5:53 AM

Post #4 of 5 (548 views)
Re: [shawnhcorey] A few more problems [In reply to] Can't Post

corn it is then well would have been if the script wasn't made obsolete by a firewall rule.

Any thing i can look up about how to compare arrays.

Thanks again

Sav


shawnhcorey
Enthusiast


Sep 28, 2009, 6:11 AM

Post #5 of 5 (545 views)
Re: [savo] A few more problems [In reply to] Can't Post


In Reply To
Any thing i can look up about how to compare arrays.


http://search.cpan.org/~vparseval/List-MoreUtils-0.22/lib/List/MoreUtils.pm#pairwise

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives