Dennis Bourn wrote:
Im working on a perl script to add the IP addresses from spam to my blocked list. Each quarentined email is kept in one directory, the IP address of the sender is in the first line of the headers. Im using O'Reilly's "Learning Perl" as a guide and got some parts working. I can list all the files in the directory eaisy enough,.. i was going to have that script supply the filename and fire off another script which searched for the IP in individual files,.. I think its a waste of processing power to scan the entire file when i just need the first line. Is there an eaisy way to limit the search to the first line?

some code
the first one returns each filename in the directory.
----------------------------
#!/usr/bin/perl -w
use strict;
my $file;
my $dir = "mail/";
opendir(BIN, $dir) or die "Can't open $dir: $!";
while( defined ($file = readdir BIN) ) {
next if $file =~ /^\.\.?$/;     # skip . and ..
   print "$file\n" if -T "$dir/$file";
}
closedir(BIN);
-----------------------------

the second one searches a single file for IP addresses. (I left in my failed attempts to get the regex correct,.. figured it might make someone happy to know that there are worse coders out there than themselves)
------------------------------
#!/usr/bin/perl -w
use strict;
while (<>){
 if (/\d+\.\d+\.\d+\.\d+/){
 #if (/\(*\)/){    #search for anything in brackets
 #if (/\d\.\d\.\d\.\d/){  # search for digit.digit.digit.digit
 #if (/([1-255]\.[0-255]\.[0-255]\.[0-255])/) { #another atempt
   print "$&\n";
              }
}
------------------------------

I finally made it to page 132 in "Learning Perl" and discovered the command line syntax.

perl -n -w -e 'if (/\d+\.\d+\.\d+\.\d+/){ print "$&\n";}' mail/mail*

that single command does most of what i need, I can just pipe the output to pfctl to add it to the firewall block list. however i still have the problem of it checking the entire file instead of the first line. In addition im seeing a version number in the headers like V6.2000.20.5 which matches my regular expresion. If i can narrow it down to the first line of the file this wont be a problem.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to