troubleshooting Question

Modification of read-only error PERL

Avatar of marcparillo
marcparillo asked on
Scripting Languages
3 Comments1 Solution258 ViewsLast Modified:
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
Kim Ryan
IT Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros