dru145 has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks,

I have this script almost completed. I have a foreach loop that will subsitute my firewall's ip address with x's and also only include the first 15 lines from the firewall log in the email that I send to the sysadmin (as evidence). Within the here document, the foreach loop is not completely working. It includes the full snapshot without replacing the firewall's ip address and it does not cut off at 15 lines. If I just print it to STDOUT, then everything is kosher. Can somebody let me know what I am doing wrong? I tried everything that I can think of. Thanks in advance,

-Dru
---------First Part of Script Not Included------------- my $ip = qr/\b$ips[0][0]\b/o; my @fwlog; my $logfile = "/home/jdoe/scripts/fwanalysis/05jul01.drop"; open LOG, "$logfile" or die "Can't open $logfile: $!\n"; while (<LOG>){ if ($_ =~ $ip){ push (@fwlog, $_); } #end if } #end while close LOG; my $i = 0; open(SENDMAIL, "|/usr/lib/sendmail -oi -t") or die "Can't fork for sendmail: $!\n"; print SENDMAIL <<EOM; From: root<root\@localhost> To: root<root\@localhost> Subject: Flagged Activity I am the Network Security Manager for ACME INC. While I was performing + a scan through our logs I found an ip from you network trying to hac +k us. Cease or be destroyed. Source IP Address: $ips[0][0] Sample from the firewall logs: foreach (@fwlog){ s/192.168.2.3/x.x.x.x/; print $_; if($i++ >15){ last } }#end foreach EOM close(SENDMAIL) or warn "sendmail didn't close nicely";

Replies are listed 'Best First'.
Re: Foreach loop not working within Mail
by blakem (Monsignor) on Aug 09, 2001 at 01:26 UTC
    You need to move the 'EOM' marker up above the foreach loop.... Something like:
    print SENDMAIL <<"EOM"; [snip] Sample from the firewall logs: EOM for (@fwlog) { [snip] print SENDMAIL $_; }
    As you have it written, the foreach loop is within the here doc. This is not what you want. End the here doc before the loop, then use the loop to print out the rest of the data.

    -Blake

Re: Foreach loop not working within Mail
by VSarkiss (Monsignor) on Aug 09, 2001 at 01:33 UTC

    A here-doc doesn't get executed. Variables within it are interpolated, but there's no execution. Massage the array first, then interpolate it, something like this:

    splice(@fwlog, 15); # shorten array to max 15 elems foreach (@fwlog) { s/192\.168\.2\.3/x.x.x.x/; # dots should be escaped } # ... later ... print SENDMAIL <<EOM; .... Sample from the firewall logs: @fwlog EOM
    I'm presuming the elements in @fwlog still have the newlines intact.

    HTH