Welcome to WebmasterWorld Guest from 54.160.163.163

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Do not ban Ip from admins (Perl question ) Part 2

Do not ban Ip from admins (Perl question ) Part 2

     

StopSpam

11:45 am on Jun 10, 2003 (gmt 0)

10+ Year Member




I am using the code that HTTP_Daemon had recommaned me ;-)
and it wont ban the admin ips but unfortualy it ends the script with a hard 501

here is code;

my @admin_ips = qw(18.71.4.28 12.73.4.29 4.6.2.28);
my $visitor_ip = "$ENV{'REMOTE_ADDR'}";
exit if grep(($visitor_ip eq $_),@admin_ips);

is there a way to end the script nice? not with 501 error but with print text
like something... a visitor should have gotten banend now ...

I got a second question ...

Here is the code i use to write the visitors ip ban to the htaccess file.

open(HTACCESS,"".$root."/\.htaccess") ¦¦ die $!;
chmod (0666, "$root/.htaccess");
@htaccess = <HTACCESS>;
close(HTACCESS);

open(HTACCESS,">".$root."/\.htaccess") ¦¦ die $!;
print HTACCESS "SetEnvIf Remote_Addr \^".$visitor_ip."\$ ban\n";
foreach $banip (@htaccess) {
print HTACCESS $banip;
}
chmod (0666, "$root/.htaccess");
close(HTACCESS);

Now i want to add few lines to check if the ip adress is allready
banned in the htaccess file then do not write ban line again.

some thing like if $visitor_ip is in htaccess then exit to .. dont write ban

or if $visitor_ip is in htaccess then do &close

can some one show me a example on how i should do this ...

Greets MArk ...

jatar_k

4:17 pm on Jun 11, 2003 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



all the PERL folk getting shy?

cminblues

4:05 am on Jun 12, 2003 (gmt 0)

10+ Year Member



my @admin_ips = qw(18.71.4.28 12.73.4.29 4.6.2.28);
my $visitor_ip = "$ENV{'REMOTE_ADDR'}";
exit if grep(($visitor_ip eq $_),@admin_ips);

If you want the exit sub to do something like print correct headers _or_ closing tags etc, as HTTP_Daemon stated,
you must write a custom exit sub doing this.
[If you just use exit, then your script will just exit, without printing the correct headers.]

Ok, here is an example:

my @admin_ips = qw(18.71.4.28 12.73.4.29 4.6.2.28);  
my $visitor_ip = "$ENV{'REMOTE_ADDR'}";
&myexit if grep(($visitor_ip eq $_),@admin_ips);
..
.. some lines below [or above]
..
#-- put here your closing
[but _also_ the opening ones, if you've still not done the "Content-type" stuff..] lines
sub myexit {
print "</body></html>\n";
exit 0;
}

-cminblues

[edited by: jatar_k at 12:33 am (utc) on June 13, 2003]
[edit reason] broke line for sidescroll [/edit]

cminblues

4:38 am on Jun 12, 2003 (gmt 0)

10+ Year Member



Excuse me, I've seen now the 2nd question ;)

Now i want to add few lines to check if the ip adress is allready
banned in the htaccess file then do not write ban line again.

That depends.. are you on a *nix or *sloft server?

Here are the 2 options:

lazy way [*nix server]:

my $text = 'findme'; 
if (`grep -c -m 1 -e '\Q$text\E' /path/to/file.ext` > 0) {
print "Found!\n";
}

compatible way:

my $text = 'findme'; 
open(RD,'/path/to/file.ext');
while(<RD>) {
my $line = $_;
chomp $line;
next if($line !~ /\Q$text\E/);
print "Found!\n";
last;
}
close RD;

P.S. The \Q & \E meaning, is to escape the representation of the '.' value in numeric IPs [in this case], as 'any character'.

-cminblues

broadKnowledge

12:04 am on Jun 13, 2003 (gmt 0)

10+ Year Member



Dear good coders, I just had to share this opinion with you. Your code looks great and I am sure it will ban the IPs you want. But FYI, people who try to get around htaccess are almost always very knowledgeable about utilizing proxy's to do their deeds. I just had to share that because I know of a situation where banning IPs is pointless. Sorry I don't mean to burst anyone's bubble, hopefully that may make you guys more creative about your objectives. Try resolving the IPs, detect proxy and ban.

cminblues

2:08 am on Jun 13, 2003 (gmt 0)

10+ Year Member



Try resolving the IPs, detect proxy and ban.

Sure it is a good measure to scan the IP's list of access logs, searching for proxies.

Only a note:
In this case, I think that 'resolving the IPs' is nearly useless.
[You'll get _sometimes_ the reverse-lookup entries, and after that?]

The true job, is to try the IPs with some proxy-checker.
[I talk by experience, and Vlad, I fear, has something to say about that, these days.. :)]

[added]I must correct myself: resolving the IPs _can_ be useful, but only if you, before or after, can directly test them[/added]

StopSpam

8:54 pm on Jul 23, 2003 (gmt 0)

10+ Year Member



I need help from the mighty perl guru's from WebMastersWorld ;-)

this is for unix and linux servers

why is this working: (Code 1 sample)

my @admin_ips = qw(22.33.4.55 22.33.4.55 55.44.3.11);
my $visitor_ip = "$ENV{'REMOTE_ADDR'}";
exit if grep(($visitor_ip eq $_),@admin_ips);

and this not: (Code 2 sample)

$adminip1 = "22.33.4.55";
$adminip2 = "88.77.6.55";
$adminip3 = "55.44.3.11";

my @admin_ips = qw($adminip1, $adminip2, $adminip3);
my $visitor_ip = "$ENV{'REMOTE_ADDR'}";
exit if grep(($visitor_ip eq $_),@admin_ips);

###########

this code is past above the ban code to make sure admin ips will not get banned
The (Code 1 sample) works fine, how ever the (Code 2 sample) bans the admin ip adressess as well
looks like it cant read them in from the $adminip1 = lines?

the reason imade code 2 is because the script must read the admin ips
from a settings file not the script it self as in code sample 1

i also try this without , behind it:
my @admin_ips = qw($adminip1 $adminip2 $adminip3);
is not working as well ....

Damian

11:31 pm on Jul 23, 2003 (gmt 0)

10+ Year Member



I think you should change this line

my @admin_ips = qw($adminip1, $adminip2, $adminip3);

to

my @admin_ips = ($adminip1, $adminip2, $adminip3);

The quotes that the qw(item1 item 2 item3) notation allows you to skip aren't needed in the above line

StopSpam

8:19 am on Jul 24, 2003 (gmt 0)

10+ Year Member



Thanks Damian

Now that works ... ;-)

i go look up what the qw( stands for

 

Featured Threads

Hot Threads This Week

Hot Threads This Month