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 ...
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]
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
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]
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 ....