Link to home
Start Free TrialLog in
Avatar of marcparillo
marcparillo

asked on

Modification of read-only error PERL

I've written a PERL script that reads a log file on my server and extracts the unique IP addresses that have Failed password attempts.  However, when I run it, I get the following error: "Modification of a read-only value attempted at line 31" -- which is around the two lines of code (@temp_hash{@LIST}++;@LIST2 = keys %temp_hash;) that takes the array and finds the unique numbers.

I've never run into this sort of error before... help!



#!/usr/bin/perl

# SEARCH FOR FAILED PASSWORDS AND WRITE TO IPTABLES

use CGI;
my $count = 0;
my @LIST;
my @LIST2;
my @temp_hash;
my %temp_hash;


open (F, "/var/log/secure");
@indata = <F>;
close F;


foreach $i (@indata) {
if (($i =~ /Failed/) && ($i !~ /nbcnc/)) {
($pass,$IP) = split/from/,$i;
($IP,$pass) = split/port/,$IP;
s/\s//g for $IP;
push @LIST,$IP;
}
}


@temp_hash{@LIST}++;
@LIST2 = keys %temp_hash;

open(T,"/var/www/vhosts/domain.com/httpdocs/secure.txt");
my $indata = <T>;
close T;

# NUMBER OF OLD BLOCKED IPS
my $int = 0+$indata;

for ($i = 1;$i <= $int; $i++) {
system("/sbin/iptables -D INPUT 1");
}


foreach(@LIST2) {
$count++;
system("/sbin/iptables -I INPUT 1 -s $_ -j DROP");
}
close S;

# RECORD THE NUMBER OF NEW BLOCKED IPS
open(V,">/var/www/vhosts/domain/httpdocs/secure.txt");
print V $count;
close V;

exit;
ASKER CERTIFIED SOLUTION
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
Perhaps you mean
$_++ for @temp_hash{@LIST};
or since you only seem to need the keys and not a count, perhaps
@temp_hash{@LIST}=();
Avatar of Perl_Diver
Perl_Diver

probably should be:

$_++ for @temp_hash{@LIST};
foreach my $keys (keys %temp_hash) {
   push @LIST2, $keys if $temp_hash{$keys} == 1;
}