CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Log File :: if the text INCLUDES something..

 



macuser9214
Novice

Sep 3, 2007, 10:27 AM

Post #1 of 19 (1545 views)
Log File :: if the text INCLUDES something.. Can't Post

Here's what I'm trying to do...

I have a LAMP server, and I'm running perl on it. I have an Apache log file, that is constantly updating as people visit the site.

Well I want a perl script to keep up with the file. Now, if someone access a certain part of the site, i want the perl script to print "Someone has accessed this part of the site" or whatever, to a terminal window.

I thought this would work:


Code
#!/usr/bin/perl 
use strict;
use warnings;
use File::Tail;

my $file;
my $line;

$file=File::Tail->new("/var/www/mu9214/logs/log");
while (defined($line=$file->read)) {

print 'i found it' if $line =~ /submit\.php/;


}



Now, when I run it, it stops, and waits, but when i go to submit.php (and it registers in the log) it doesn't say "i found it" in the terminal. I don't understand why it's not working.

Can anyone help?
Please consider visiting my site, at <a href="http://macuser9214.com"> macuser9214.com - Thought's Blog/Tech Forum.


KevinR
Veteran


Sep 3, 2007, 11:59 AM

Post #2 of 19 (1543 views)
Re: [macuser9214] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

it never print "i found it" even if you wait for a little while? Is the pattern you are looking for correct?

submit.php

all lowers case letters
-------------------------------------------------


macuser9214
Novice

Sep 3, 2007, 12:17 PM

Post #3 of 19 (1541 views)
Re: [KevinR] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

Yup its the right pattern.. I can't figure it out...

Thanks for your reponse.


Here's the log entry:

Code
24.209.47.228 - - [03/Sep/2007:15:19:25 -0400] "POST /feedthegeeks/pastebin/submit.php HTTP/1.1" 200 477

Please consider visiting my site, at <a href="http://macuser9214.com"> macuser9214.com - Thought's Blog/Tech Forum.

(This post was edited by macuser9214 on Sep 3, 2007, 12:20 PM)


KevinR
Veteran


Sep 3, 2007, 2:59 PM

Post #4 of 19 (1538 views)
Re: [macuser9214] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

try this:


Code
  while (defined($line=$file->read)) {  
print $line;
}


and see what gets printed
-------------------------------------------------


macuser9214
Novice

Sep 4, 2007, 12:54 PM

Post #5 of 19 (1533 views)
Re: [KevinR] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

 
Someone gave me this:

Code
#!/usr/bin/perl  
use warnings;
use strict;

my $file = '/var/www/mu9214/logs/log';
my $lastprocessed=0;
my $count;
my @lines;
my $line;

while (1) {
$count=0;
open(FILE, $file) or warn "Can't Open $file: $!";
@lines = <FILE>;
close (FILE);
foreach $line (@lines) {
$count++;
if (($line =~m/submit\.php/i) and ($count > $lastprocessed)) {
$lastprocessed=$count;
chomp $line;
print "$count: $line\n";
}
}
sleep(10); #Wait 10 seconds before checking again
}




BUT, i don't know how to make it so that it doesn't print the log file first.. Like, when i run the script, it prints all the instances of submit.php in the log, before it works... The person told me I need a counter, because I'm new and he wants me to learn, and eperiment, and look it up, but I can't figure it out. I want it to NOT print the old instances of submit.php.
Please consider visiting my site, at <a href="http://macuser9214.com"> macuser9214.com - Thought's Blog/Tech Forum.


KevinR
Veteran


Sep 4, 2007, 2:38 PM

Post #6 of 19 (1532 views)
Re: [macuser9214] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

The code you posted is very inefficient for this job especially if the log file is big (as log files can get). Reading the entire file into an array every 10 seconds is just a bad way to do this. But before I try and help anymore I need to be clear on what you are trying to do.

What do you want to print? Just the last line that contains submit.php in the log file? Or something else?
-------------------------------------------------


macuser9214
Novice

Sep 4, 2007, 3:02 PM

Post #7 of 19 (1529 views)
Re: [KevinR] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

Well, like I said someone made it for me...


Ok, like I said, it's an apache log...

When someone goes to a any part of my website, it logs it.

When someone goes to submit.php, i want it to print "someone has posted to the pastebin"


Do you think file::tail would be better? Like I said, I couldn't get it to work..
Please consider visiting my site, at <a href="http://macuser9214.com"> macuser9214.com - Thought's Blog/Tech Forum.


macuser9214
Novice

Sep 4, 2007, 3:06 PM

Post #8 of 19 (1528 views)
Re: [KevinR] Log File :: if the text INCLUDES something.. [In reply to] Can't Post


In Reply To
try this:


Code
  while (defined($line=$file->read)) {  
print $line;
}


and see what gets printed



That doesn't print anything. It just stops, like it's waiting... but prints nothing.. And yes, the path is correct.
Please consider visiting my site, at <a href="http://macuser9214.com"> macuser9214.com - Thought's Blog/Tech Forum.


KevinR
Veteran


Sep 4, 2007, 7:19 PM

Post #9 of 19 (1527 views)
Re: [macuser9214] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

If you don't set an interval rate, I think File::Tail waits one minute before checking the log file again. Personally I think File::Tail is the way to go. But you can use the code you posted although it is no where near as good as the module.
-------------------------------------------------


KevinR
Veteran


Sep 4, 2007, 8:50 PM

Post #10 of 19 (1525 views)
Re: [macuser9214] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

I tried File::Tail and it seemed to work OK to me. You're not trying to run your program as a CGI script are you?
-------------------------------------------------


macuser9214
Novice

Sep 5, 2007, 4:01 AM

Post #11 of 19 (1522 views)
Re: [KevinR] Log File :: if the text INCLUDES something.. [In reply to] Can't Post


Code
  while (defined($line=$file->read)) {   
print $line;
}


This prints a few lines of the log after one minute. But the other way I had it still does not work, even after a minute.
Please consider visiting my site, at <a href="http://macuser9214.com"> macuser9214.com - Thought's Blog/Tech Forum.


KevinR
Veteran


Sep 5, 2007, 9:55 AM

Post #12 of 19 (1520 views)
Re: [macuser9214] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

Try this code:


Code
#!/usr/bin/perl  
use strict;
use warnings;
use File::Tail;

$|=1; # unbuffer output;
my $name = '/var/www/mu9214/logs/log';

my $file = File::Tail->new(name=>$name, maxinterval=>10) or die "$!";
while (defined(my $line = $file->read)) {
if ($line =~ m/submit\.php/i) {
print "sumbit.php has been found in: $line";
last; #end the "while" loop
}
else {
print "Not found, continuing, wait about 10 seconds.\n";
}
}
print "exiting program......";
exit;


This will end the loop and exit after the first instance of "submit.php" is found. If it appears to work, remove the "last" in the loop and try again. Be patient, but I did set it to try every 10 seconds so hopefully you see some results faster after the initial try which by default is 10 seconds according to the documentation. So it should take approx 20 seconds after the scripts starts and the file is read to first display anything.

When I ran File::Tail yesterday I could see entries being appended to the log while the script ran. I accessed some pages on the test site that did not exist to generate some 404 (not found) lines in the log file and they would show up after a brief delay.
-------------------------------------------------


macuser9214
Novice

Sep 5, 2007, 12:27 PM

Post #13 of 19 (1517 views)
Re: [KevinR] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

It worked!!!



Cool.. Now is there any way to used regex somehow (i don't know anything about regex) and make it say:

Submit.php has been accessed by (ip address)

Thanks a lot!

I think I need:

$line =~ \b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b;

But I don't know where to put it....
Please consider visiting my site, at <a href="http://macuser9214.com"> macuser9214.com - Thought's Blog/Tech Forum.

(This post was edited by macuser9214 on Sep 5, 2007, 12:33 PM)


KevinR
Veteran


Sep 5, 2007, 3:59 PM

Post #14 of 19 (1512 views)
Re: [macuser9214] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

What is the nature of this project you are working on? Why are you doing it?
-------------------------------------------------


macuser9214
Novice

Sep 5, 2007, 4:05 PM

Post #15 of 19 (1509 views)
Re: [KevinR] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

Eventually, I'm going to put it in an IRC bot I'm working on... But for now, I need to get the other stuff out of there..... I just need the IP.
Please consider visiting my site, at <a href="http://macuser9214.com"> macuser9214.com - Thought's Blog/Tech Forum.


KevinR
Veteran


Sep 5, 2007, 5:04 PM

Post #16 of 19 (1508 views)
Re: [macuser9214] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

if the line is like this:

24.209.47.228 - - [03/Sep/2007:15:19:25 -0400] "POST /feedthegeeks/pastebin/submit.php HTTP/1.1" 200 477

($ip) = $line =~ /^\s*(\S+)/;
print $ip;

you can remove \s* if there are no leading spaces.\S just means non space characters. So all you need is to match all the non spaces at the beginning of the line to get the IP. Or you could do some minor validation:

($ip) = $line =~ /^\s*([\d.]+)/;

which will return any number of digits and dots that are at the beginning of the string.
-------------------------------------------------


macuser9214
Novice

Sep 5, 2007, 5:48 PM

Post #17 of 19 (1506 views)
Re: [KevinR] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

Sorry to be a pain in the ***, but where do I put it, I tried to put it in a few spots, and got a bunch of errors when i tried to load it..

Thanks so much for all your help!
Please consider visiting my site, at <a href="http://macuser9214.com"> macuser9214.com - Thought's Blog/Tech Forum.


KevinR
Veteran


Sep 6, 2007, 12:10 AM

Post #18 of 19 (1505 views)
Re: [macuser9214] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

it can be done in one operation:


Code
while (defined(my $line = $file->read)) {   
if ($line =~ m/^([\d.]+).*?submit\.php/i) {
print $1;
# print "sumbit.php has been found in: $line";
last; #end the "while" loop
}
else {
print "Not found, continuing, wait about 10 seconds.\n";
}
}

-------------------------------------------------


macuser9214
Novice

Sep 9, 2007, 5:44 PM

Post #19 of 19 (1498 views)
Re: [KevinR] Log File :: if the text INCLUDES something.. [In reply to] Can't Post

I'm using

Code
#pastebin 
$|=1; # unbuffer output;
my $name = '/var/www/mu9214/logs/log';

my $file = File::Tail->new(name=>$name, maxinterval=>2) or die "$!";
while (defined(my $line = $file->read)) {

if ($line =~ m/^([\d.]+).*?\/pastebin\/submit\.php/i) {

print "Something has been pasted in the pastebin... by IP address: $1\n\n";

}
}

#end pastebin


in my IRC bot script, and it's throwing off the bot. It's really weird.. Sometimes it will work as normal, and other times, it won't work at all... Mostly the latter. It doesn't work at all...

Can you help me? Do you want my bot script ( i'll PM it to you if you want. )

But thanks. Without the bot, it works fine.
Please consider visiting my site, at <a href="http://macuser9214.com"> macuser9214.com - Thought's Blog/Tech Forum.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives