need help writing a script

Hello everyone. Well, I will get right to the point. I am new to Perl and trying to learn it as much as I can. I have been assigned the task of writing a perl script to extract information from firewall logs.

Like I said, I am new to Perl and I am having a tough time because I think what I am trying to do is very advanced.

Here is what I want to do:I am trying to extract information from a firewall log and put it into a new file. I only want to extract certain pieces of information. Here are a few examples:

Let me show what I am trying to pull out of the file:

03/13/03 16:44:56 kernel Temporarily blocking host 212.241.116.21

(This is where the inital block occurs, the firewall will then continue to block all attempts from this IP address.I would like to extract all entries in the firewall like this one.)

03/13/03 16:44:57 firewalld103 deny in eth0 48 tcp 20 117 212.241.11.21 209.126.xxx.xxx 4449 80 syn (LO-Proxied-HTTP)

(At this point, the firewall continues to block the attempt. I would like to extract all lines in the firewall that contain this as well...contains very useful information such as ports and packet sizes.)

With that in mind, can I ask for someone to help me build my script? I feel like I am butting my head against a wall. I know I have much to learn, but can learn a lot from seeing the script and breaking it down to see how it functions.

I really need help. If someone can help me write this script, I would be extremely grateful. I should mention, this is not a homework assignment. :slight_smile:

Also, once I see the script and how it is written, I believe it will help me understand much more.

Thanks.

Tarballed

Here's a rough and ready version; as always, check that the location of your perl program is correct after the "#!" :

#!/usr/bin/perl -w

while ( <> ) {
    if ( m/blocking/i | m/deny/i ) {
        print;
    }
}

How do you execute it I hear you cry? Save it as a file called firewall_extract.pl, make it executable, and it should be as follows:

./firewall_extract.pl <path_to_firewall_log >file_for_output

:slight_smile:

Edit: Learning Perl by Oreilly is a fantastic way to start learning Perl!

sometimes shell scripting is easer and much more convient then something like perl.

you could also do this.

egrep deny\|blocking /var/adm/firewall.log

yes win does have it. but IMHO i would add the following lines to make it a bit more clear.

[code]
#!/usr/bin/perl
#
# OBJECTIVE:
# 1) Open the firewall log and find all occuances of "blocking" or "deny"
#

$FILE='<path to your firewall log>';

open (FIREWALL_LOG, "$FILE") or die "Can not read file ($!)";

while (<FIREWALL_LOG>) {
chomp; # remove end of line
if ($_ =~ /(blocking|deny)/ ) {
print $_
}
}

WIntellect's version is correct but verbose. In perl:

perl -lane '/\b(blocking|deny)\b/i && print' logfilename

This beats Optimus_P's solution by only choosing the lines that have the terms you're looking for as whole words (a failing of egrep). If your logfile is huge (multimegabytes) you can gain a slight performance advantage with this:

perl -lane '/\b(?:blocking|deny)\b/i && print' logfilename

Re: Learning Perl: This is a really good book, but if you're an experienced programmer, the man pages are sufficient. Read 'man perl' thouroughly, and you'll learn the best order in which to go through them.