Re: Restrict IP access to a Perl application
- From: "John W. Krahn" <someone@xxxxxxxxxxx>
- Date: Wed, 30 Jan 2008 07:45:15 GMT
barramundi9 wrote:
I am a newbie to Perl and have an application written in Perl. I put
IPs that are "allowed" to access the application into a file called
"ip.allow".
I then tried to compare the $ENV{REMOTE_ADDRESS} to the IPs in
"ip.allow" to determine the access right which looks like the
following:
10.0.0.1
10.0.0.2
10.0.0.3
And the code is:
Don't forget:
use warnings;
use strict;
$address=$ENV{'REMOTE_ADDR'};
open(FILE,"/path/to/ip.allow") or die ("Cannot open file!");
flock(FILE,2);
while ($line=<FILE>) {
$line=~s/\./\\\./g;
if ($line =~ /$address/) {
print "IP matched!!\n";
last;
}
}
flock(FILE,8);
close(FILE);
But it doesn't seem to work because when I take out 10.0.0.1 from the
ip.allow file, 10.0.0.1 can still access the application.
Any suggestions are appreciated, thanks.
You probably don't want to use a regular expression. This should work better:
while ( my $line = <FILE> ) {
chomp $line;
if ( $line eq $address ) {
print "IP matched!!\n";
last;
}
}
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.
- References:
- Restrict IP access to a Perl application
- From: barramundi9
- Restrict IP access to a Perl application
- Prev by Date: Restrict IP access to a Perl application
- Next by Date: Re: Restrict IP access to a Perl application
- Previous by thread: Restrict IP access to a Perl application
- Next by thread: Re: Restrict IP access to a Perl application
- Index(es):