Re: Restrict IP access to a Perl application



On Tue, 29 Jan 2008 23:04:26 -0800, barramundi9 wrote:

Dear all:

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:

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

1) You don't chomp the input line, so it still contains a \n
2) You can just compare strings, no need for the regexp
3) You forgot to anchor your regexp (/^$address$/), but see 2)

HTH,
M4
.



Relevant Pages

  • Re: A couple of questions from a Ruby neophyte
    ... Ruby has the One-click installer, which you can get from www.rubyforge.org ... Perl and Python have ActiveState versions of the same. ... Perl has the best regexp engine, ...
    (comp.lang.ruby)
  • Re: Need help reading a perl regexp - someone clue me?
    ... (FWIW, I'm not a Perl programmer, and the regexp ... Regexp documentation - Either the package is written in Perl, ... of digits... ...
    (comp.lang.perl.misc)
  • Re: Parsing a file that has labels
    ... > Let me explain the above perl code. ... there's something to know about perl regexps. ... As such a character doesn't exist that means the whole ... replace every occurrence of the regexp, ...
    (comp.unix.shell)
  • Re: Parsing a file that has labels
    ... Let me explain the above perl code. ... As such a character doesn't exist that means the whole ... replace every occurrence of the regexp, ...
    (comp.unix.shell)
  • SUMMARY: Global Find/Replace Question...
    ... The two popular ways of doing this are Perl and Sed/Grep Script.. ... I am summarizing the following responses that I received: ... REGEXP can be any valid perl regular expression. ... Note that REGEXP can be any valid sed regular expression. ...
    (SunManagers)