Gossamer Forum
Home : General : Perl Programming :

Simple IP banning routine

Quote Reply
Simple IP banning routine
I have a goofy problem. I have a list if IP addresses (and partial IP addresses) that I want to refuse from using a little script I wrote. The IP banning feature seemed to work for a little while but now that the list has grown to about 150 IP addresses, it doesn't work.

The list is just one IP per line.

$ip would be the users current IP.

Code:
foreach $bannedip (@blist) {
if($ip =~ /$bannedip/gi) {
open(HTML, "$bannedpage") || print "Can't open $bannedpage\n";
@html_text = <HTML>;
close(HTML);
print @html_text;
exit;
}
Quote Reply
Re: [BennyHill] Simple IP banning routine In reply to
When you say it doesn't work, what happens?

Btw your i in the regex is redundant as numbers and periods don't have different cases :)
Quote Reply
Re: [Paul] Simple IP banning routine In reply to
Oops, I did the same thing for the domain banning so I suppose I forgot to remove the "i". Anyway, as you can see, if the IP were on the list, it should just go to an error page which it doesn't do. I can ban my IP and try posting with the script and it lets me through every time.
Quote Reply
Re: [BennyHill] Simple IP banning routine In reply to
The only thing I can think of is that the users IP does not get passed to $bannedip. Is there someway you can double check this? I'd suggest putting a print $bannedip; in there to see if it gets passed properly, as your code looks kosher.

- wil
Quote Reply
Re: [BennyHill] Simple IP banning routine In reply to
Try adding some debuggings stuff;

Code:
foreach $bannedip (@blist) {
print "Comparing $ip and $bannerip<BR>";
if($ip =~ /$bannedip/g) {
open(HTML, "$bannedpage") || print "Can't open $bannedpage\n";
print "Opened file ok...<BR>";

@html_text = <HTML>;
print "Got \@html_text<BR>";

close(HTML);
print "Closed data file connection...<BR>";
print @html_text;
exit;
}
It may not solve your problem...but it may point you in the right direction as to why its not working ;)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [BennyHill] Simple IP banning routine In reply to
You can do as wil said...I'd use something like:

Code:
my @list_of_ips = qw/111.111.111.111 222.222.222.222/;

for (@list_of_ips) {
if (/\Q$ENV{REMOTE_ADDR}\E/) {
print "WE HAVE A MATCH $_";
last;
}
}

Edit: Posted before I saw Andys post.

Last edited by:

Paul: Apr 16, 2002, 8:51 AM
Quote Reply
Re: [Paul] Simple IP banning routine In reply to
>>>Edit: Posted before I saw Andys post. <<<

Yup..same as with Wil... forum editor wouldn't line up the Perl code right :(

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy.] Simple IP banning routine In reply to
Thanks guys. I really appreciate it. $bannedip was fine but it turned out to be a blank line in the IP list that was screwing everything up. Everything after that blank line was being allowed. I have no clue why but I made sure no more blank lines could work their way in to the list.
Quote Reply
Re: [BennyHill] Simple IP banning routine In reply to
 
Code:
foreach $bannedip (@blist) {

chomp ($bannedip); # remove \n
if (!$bannedip) { next }; # skip blank lines

if($ip =~ /$bannedip/g) {
open(HTML, "$bannedpage") || print "Can't open $bannedpage\n";
@html_text = <HTML>;
close(HTML);
print @html_text;
exit;
}

- wil
Quote Reply
Re: [Wil] Simple IP banning routine In reply to
Hmm I think you'd want \Q and \E in that regex unless the ips contain meta-characters for wild-carding.

You could do a print while <HTML>; rather than slurping everything into an array.

Thinking about it, the g in the regex isn't needed either. A match is a match, there is nothing to check globally.

Last edited by:

Paul: Apr 17, 2002, 2:51 AM
Quote Reply
Re: [Paul] Simple IP banning routine In reply to
Yeah, I know. The OP code isn't exactly Larry's standards, but you may have noticed that I've left it as-is. I've just added two extra lines in there. I wasn't going to start to unwrangle and crriticize the OP code - I'd be here all day ;-)

- wil
Quote Reply
Re: [Wil] Simple IP banning routine In reply to
Guess I'll have to then :)

Code:
my @blist = __read ips from file__;
my $found;

foreach (@blist) {
chomp;
next if /^(#|\s)*$/;
/\Q$_\E/ and $found = 1, last;
}

if ($found) {
open(HTML, "$bannedpage") or die "Can't read $bannedpage : $!";
print while <HTML>;
close(HTML);
exit;
}

That probably has errors in it too...ah well Smile I think [# ] is faster than (#|\s)

Last edited by:

Paul: Apr 17, 2002, 3:30 AM
Quote Reply
Re: [Paul] Simple IP banning routine In reply to
Code:
if ($found) {
local $/;
open(HTML, "$bannedpage") or die "Can't read $bannedpage : $!";
print while <HTML>;
close(HTML);
exit;
}

:-)

- wil
Quote Reply
Re: [Wil] Simple IP banning routine In reply to
Err you don't need it ;)
Quote Reply
Re: [Paul] Simple IP banning routine In reply to
Yeah, it enables slurp mode and speeds things up considerably. Bench it :-)

- wil
Post deleted by Paul In reply to

Last edited by:

Paul: Apr 17, 2002, 4:06 AM
Quote Reply
Re: [Wil] Simple IP banning routine In reply to
As you can see, it does start to make a difference as you do more and more:

Edit:

Well I was going to show the benchmark but I did so many iterations it crashed my pc :)
Quote Reply
Re: [Paul] Simple IP banning routine In reply to
LOL! Try in on a FreeBSD box instead ;-)

- wil
Quote Reply
Re: [Wil] Simple IP banning routine In reply to
I'm kind of wrong here, actually.

It makes a bigger difference if you're reading the file into an array or another form of memory - as the name implies, when you're 'slurping' the file in. Using a WHILE loop I don't know what or how much difference it would make.

- wil
Quote Reply
Re: [Wil] Simple IP banning routine In reply to
A while loop isn't slurping into memory, whereas something like:

my @array = <FILE>;

...is. Thats why you'd need local $/ (well not just for that).

Last edited by:

Paul: Apr 17, 2002, 4:14 AM