Re: My first Perl script
by CukiMnstr (Deacon) on Apr 10, 2003 at 00:42 UTC
|
I don't know what you mean by "nice and neat"? Do you want only a part of the information from each line in firewall.txt? If so, then you probably want to check split(), or maybe want to use regular expressions. You will have to give us an example of "very ugly and not very clean" and then an example of "nice and neat" if you want more help...
I have a comment on your script as it is: you are reading all of firewall.txt into memory, which can bring problems if the file gets very large. It's better if you open it and then iterate through it searching the lines that interest you. It's also a good idea to check the return values of your open() calls:
open(FW_LOG, $firewall_log)
or die "$firewall_log open() failed: $!";
open(MYNEWFILE, ">>$my_new_log")
or die "$my_new_log open() failed: $!";
while ($line = <FW_LOG>) {
if ($line =~ /kernel Temporarily blocking host/ || $line =~ /block
+ed/) {
# your line transformations would go here...
print MYNEWFILE $line;
}
}
close(FW_LOG) or die "$firewall_log close() failed: $!";
close(MYNEWFILE) or die "$my_new_log close() failed: $!";
hope this helps,
update: added closing curly brace for if, removed colon at the end of filename $my_new_log | [reply] [d/l] [select] |
|
|
Ahh, I was able to modify my code with you what suggested. Here is the new code:
#!/usr/bin/perl
#Variables
$firewall_log = "/home/jwilliams/firewall.txt";
$my_new_log = "/home/jwilliams/extracted_log";
open(FW_LOG, $firewall_log)
or die "$firewall_log open() failed: $!";
open(MYNEWFILE, ">>$my_new_log:")
or die "$my_new_log open() failed: $!";
while ($line = <FW_LOG>) {
if ($line =~ /kernel Temporarily blocking host/ || $line =~ /block
+ed/) {
# your line transformations would go here...
print MYNEWFILE $line;
}
}
close(FW_LOG);
close(MYNEWFILE);
Now, I just need to figure out how to clean up the extracted info.
T. | [reply] [d/l] |
Re: My first Perl script
by Coruscate (Sexton) on Apr 10, 2003 at 01:10 UTC
|
To link the 2 nodes together, the original node of this thread is the result of efforts posted at node id 249322, Extracting data from a firewall log.
If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.
| [reply] |
Re: My first Perl script
by Nkuvu (Priest) on Apr 10, 2003 at 00:44 UTC
|
Yes. :)
If you want more detail then perhaps you might want to explain exactly what it is that isn't neat, and what would look neat to you...
| [reply] |
Re: My first Perl script
by tarballed (Initiate) on Apr 10, 2003 at 16:21 UTC
|
I do apologize for not posting what the results are. I will try and post a bit in here, see if it shows up like it does in the file.
Does not look like it will let me post in here without it going all crazy. If I can explain. basically, the results have a lot of spaces inbetween each piece of text that was extracted from the log.
Here is a little snip:
1066268 04/04/03 22:07:27 y kernel Temporarily blocking host 209.178.198.139
Some space
1066318 04/04/03 22:07:30 n deny in eth0 48 tcp 20 115 209.178.198.139 209.126.131.14 2234 80 syn (blocked site)
As you can see, the results are spaced inbetween the fields and between each new line of text extracted. I hope this helps.
Tarballed | [reply] |
Re: My first Perl script
by tarballed (Initiate) on Apr 10, 2003 at 16:23 UTC
|
Hmm, well that was funny. The second piece of text that I posted into this thread came out exactly the way I wanted it to.LOL. It does not look like that in the file. I will try again.
info (lots of space) more info (lots of space) more info (lots of space) yet more info
It is not clean like what I posted by accident in the previous update.
Tarballed | [reply] |
Re: My first Perl script
by KPeter0314 (Deacon) on Apr 10, 2003 at 17:04 UTC
|
Oops, not a real reply. Playing with formatting and didn't intend to hit submit | [reply] |
Re: My first Perl script
by tarballed (Initiate) on Apr 10, 2003 at 18:57 UTC
|
Back again: I have some more questions. I also wanted to ask about cukimunsters help on my script. Here is the suggestion:
open(FW_LOG, $firewall_log)
or die "$firewall_log open() failed: $!";
open(MYNEWFILE, ">>$my_new_log:")
or die "$my_new_log open() failed: $!";
while ($line = <FW_LOG>) {
if ($line =~ /kernel Temporarily blocking host/ || $line =~ /block
+ed/) {
# your line transformations would go here...
print MYNEWFILE $line;
}
close(FW_LOG) or die "$firewall_log close() failed: $!";
close(MYNEWFILE) or die "$my_new_log close() failed: $!";
Now, I was playing around with this code. (Granted, I am very new and raw to Perl so please bear with me.) I was trying to figure out how to implement this code and learn from it, but for some reason, I cannot seem to get it working. It is probably because I have a TON of information on Perl to learn, but I am working on it. Anyways, I tried to add some code from my original script and this is what I got: (Note, I cant get this script to work though :/ )
#!/usr/bin/perl
#Variables
$firewall_log = "/home/jwilliams/firewall.txt";
$my_new_log = "/home/jwilliams/extracted_log";
open(FW_LOG, $firewall_log)
or die "$firewall_log open() failed: $!";
open(MYNEWFILE, ">>$my_new_log:")
or die "$my_new_log open() failed: $!";
while ($line = <FW_LOG>) {
if ($line =~ /kernel Temporarily blocking host/ || $line =~ /block
+ed/) {
# your line transformations would go here...
print MYNEWFILE $line;
}
close(FW_LOG) or die "$firewall_log close() failed: $!";
close(MYNEWFILE) or die "$my_new_log close() failed: $!";
If anyone has some thoughts, feel free to. I have learned a tremendous amount about Perl in just the last day from being here.
Secondly, what steps would anyone recommend take for me to fully learn and understand Perl so I can utilize it more efficiently? I appreciate all comments and suggestions.
Tarballed | [reply] [d/l] [select] |
|
|
Why do you have the : in open(MYNEWFILE, ">>$my_new_log:") ?
And it looks like you're missing a closing curly brace... you open one for the while loop, open one for the conditional, then close only one...
And how is it not working? Is it not compiling, not providing the results you expect, not providing any results, or...?
Are you 100% positive that you don't need to worry about case sensitivity when checking for "kernel Temporarily blocking host" and "blocked" ? If not, add an i after the trailing /. So it would look like $line =~ /kernel Temporarily blocking host/i
Finally, I always like to suggest the Llama book for perl beginners. Hope some of this helps...
| [reply] [d/l] [select] |
|
|
Hello there. Let me put in some revised code that I have done:
#!/usr/bin/perl
#Variables
$firewall_log = "/home/jwilliams/firewall.txt";
$my_new_log = "/home/jwilliams/extracted_log";
open(FW_LOG, $firewall_log)
or die "$firewall_log open() failed: $!";
open(MYNEWFILE, ">>$my_new_log:")
or die "$my_new_log open() failed: $!";
while ($line = <FW_LOG>) {
if ($line =~ /kernel Temporarily blocking host/ || $line =~ /block
+ed/) {
# your line transformations would go here...
print MYNEWFILE $line;
}
}
close(FW_LOG);
close(MYNEWFILE);
You are correct. The original reason why it was not working was there was a missing }. Once I placed that in, it was working.
I will add the i behind the trailing / just to make sure. Better to be cautious, than not cautious at all.
As far as the: open(MYNEWFILE, ">>$my_new_log:" is concerned, it was something that was suggested to me. Is it not needed? Do you have other recommendations?<p?
Also, when the data is extracted, it somes out kinda clumpy. It is not nice and clean. What I mean is there are spaces between the data as well as spaces between each new line. For example:
data (spaces) data (spaces) data (spaces) data (spaces)
Spaces
New line like above
I was trying to get all the data to be smooth and clean.
Yes, I just picked up the Llama book today. I am planning on also ordering the Programming Perl and Perl Cookbook today as well. The more books, the better.
Thanks for your help. It was great. Hopefully my input will help as well.
| [reply] [d/l] |
|
|