Rob Dixon am Freitag, 4. August 2006 16:54:
> Tim Wolak wrote:
> > I have modified the code however its still not writting to the file....
> > Not sure what is going on, I have tried different ideas to get it to
> > pint to the file but so far no go. Does anyone have any ideas?
> >
> > #!/usr/bin/perl -w
> >
> > use strict;
> > use warnings;
> > my $logfile = '/var/log/messages';
> > my $secv = '/var/log/secv';
> > my $hosts = '/etc/hosts.deny';
> > my $cody = '/etc/hosts.txt';
> > my @boxes;
> > my $box;
> >
> > open(LOG, $logfile) || die "Cannot open logfile for reading: $!";
> > open(SEC, '>$secv') || die "Can't open file!: $!";
> > open(HOST, $hosts) || die "Can't open file!: $!";
> > open(DENY, '>>$cody') || die "Can't open file!: $!";
> >
> > foreach (<HOST>) {
> > push @boxes, $1 if /(\d+\.\d+\.\d+\.\d+)/;
> > }
> > close HOST or die $!;
> >
> > while (<LOG>){
> > next unless /Failed password for invalid/;
> > print SEC "Invalied user logon attempt!:$_\n";
> > next unless /(\d+\.\d+\.\d+\.\d+)/;
> > my $tim = $1;
> > foreach $box (@boxes) {
> > if ($box eq /$tim/){
> > next;
> > } else {
> > print DENY "$tim\n";
> > }
> > }
> > }
> > close SEC or die $!;
> > close DENY or die $!;
> > close LOG or die $!;
>
> I think you want:
>
> foreach my $box (@boxes) {
>
> if ($box eq $tim){
> print DENY "$tim\n";
> last;
> }
> }
Hello Tim,
The "last;" statement improves the performance by omitting unneccessary tests
with further @boxes. I overlooked this; even more performant (and shorter)
would be to use a lookup hash:
# [...]
my %hosts; # instead of @hosts
foreach (<HOST>) {
$hosts{$1}=1 if /(\d+\.\d+\.\d+\.\d+)/;
}
# [...]
# after the "next unless /(\d+\.\d+\.\d+\.\d+)/;" line:
print DENY "$tim\n" if exists $hosts{$1};
} # while (<LOG>)
# [...]
Dani
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>