...

View Full Version : CGI "tail" script somewhat working



supanatral
02-27-2010, 08:22 PM
I'm trying to create a script much like the tail command for linux that views the bottom of a working log. When there is extra information added to the log, it will append at the bottom of the screen.

I took a working cgi script and all I did was add "tail -f /var/log/maillog" at the end of it and much to my surprise, it worked! After a day however I actually read the information on the log and I realized that it's missing information.

On the real log file, it will have information stored in <> brackets and whatever was in-between those brackets wouldn't be shown on the web. So for instance, in the log file, the information would look like:

from=<sender@domain.com> to=<recipient@ourdomain.com>

However, if your reading the output from the cgi script, all that would be shown is:
from= to=

Why is this?

FishMonger
02-27-2010, 09:57 PM
The < > angle brackets are used for html tags. If you need to display the literal < > angle brackets, then you need to either html encode them or put your tail output inside a set of <pre> </pre> tags.

supanatral
03-01-2010, 04:47 PM
It appears to already be selected in a previous line. When I turn it off, I loose all formatting. When I changed the line to:

<PRE>tail --lines=60 -f /var/log/maillog</PRE>

nothing shows up at all.

FishMonger
03-01-2010, 04:52 PM
Can you post your script so I can see exactly what you're doing?

supanatral
03-02-2010, 01:09 AM
That would be a good idea. I've taken the code from another cgi script I downloaded and I added the "tail" part of it. I've changed the .cgi file into a .txt file so I could upload it.

FishMonger
03-02-2010, 01:58 AM
I'm a Perl programmer and my shell scripting knowledge is very limited, so I don't think I'll be able to help in adjusting your script.

If you were using Perl, I'd direct you to the File::Tail module and for the cgi aspects I'd direct you to the CGI module.

http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm

http://search.cpan.org/~lds/CGI.pm-3.49/lib/CGI.pm

supanatral
03-02-2010, 04:20 AM
I got the CPAN module to work however it has the same problem and only shows up:
"from= to=" instead of saying "from=sender@domain.com to=me@mydomain.com"

FishMonger
03-02-2010, 04:37 AM
Please post your Perl script and we'll see what we can figure out.

supanatral
03-02-2010, 02:39 PM
Here it is:


#!/usr/bin/perl -w
print "Content-type: text/html\n\n";

use File::Tail;

$file=File::Tail->new("/var/log/maillog");

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


The fact that it's doing the same exact thing though tells me that it might be a problem with using perl as a language in this scenario.
Here is a copy and paste of what shows up in the log as compared to what is shown on the web page.
Actual log:

Mar 2 09:41:41 mailgate2 postfix/smtpd[23598]: NOQUEUE: reject: RCPT from 201-75-147-118-sc.cpe.vivax.com.br[201.75.147.118]: 550 5.1.1 <ramsey@mydomain.com>: Recipient address rejected: User unknown; from=<ogupubos9672@vivax.com.br> to=<ramsey@mydomain.com> proto=ESMTP helo=<vivax.com.br>

web page:

Mar 2 09:41:41 mailgate2 postfix/smtpd[23598]: NOQUEUE: reject: RCPT from 201-75-147-118-sc.cpe.vivax.com.br[201.75.147.118]: 550 5.1.1: Recipient address rejected: User unknown; from= to= proto=ESMTP helo=

If you notice, anything inside <> brackets is missing all together

FishMonger
03-02-2010, 03:59 PM
The fact that it's doing the same exact thing though tells me that it might be a problem with using perl as a language in this scenario.
The problem is not due to Perl. It's the HTML protocol and how browsers render the data.

No matter which language you use, you'll need to escape (html encode) the angle brackets.

Try this version.

#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use File::Tail;

my $cgi = CGI->new;
print $cgi->header, $cgi->start_html;

warningsToBrowser(1);
$|++;

my $file = File::Tail->new("/var/log/maillog");

while ( my $line = $file->read ) {
print $cgi->escapeHTML($line);
}

$cgi->end_html;

supanatral
03-02-2010, 04:12 PM
It works!!!! thank you!

The only problem, is that it's not formatted like the old script (I think it had to do with the <PRE></PRE> command). It's not necessarily important, however would be very nice. I tried playing around with it to see if I could add the <PRE> scripts but it seems to error every time I do that.

Thanks again!

FishMonger
03-02-2010, 04:16 PM
You could try this:

while ( my $line = $file->read ) {
print $cgi->pre($line);
}

supanatral
03-02-2010, 04:36 PM
That's fixes the formatting however it has the same issues with missing data in the <> fields. If it can't be done, then I guess all I'd have to do is write an extra empty line after each line from the maillog so that mentally, your able to separate it.

FishMonger
03-02-2010, 04:46 PM
What's wrong with the formatting?

Is everything displaying as a single line? If so, then we simply need to add a <br> tag.

print $cgi->escapeHTML($line), $cgi->br;

Or, you could do this:

print $cgi->pre( $cgi->escapeHTML($line) );

supanatral
03-02-2010, 04:54 PM
With the first set of code, it would show the line (which may take lets say 1 1/2 lines across the screen), then the next line of code would start where that one left off so very quickly, you would loose yourself in it.

That last code works great! it won't show the entire line on the log using one line in the browser, but at least it starts on a new line for each line of code it puts on the screen.

For those stumbled upon this looking for the answer here is a summary of the code that FishMonger helped me with:


#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use File::Tail;

my $cgi = CGI->new;
print $cgi->header, $cgi->start_html;

warningsToBrowser(1);
$|++;

my $file = File::Tail->new("/var/log/maillog");

while ( my $line = $file->read ) {
##The first one works, but my preference is the second one.
# print $cgi->escapeHTML($line), $cgi->br;
print $cgi->pre( $cgi->escapeHTML($line) );
}

$cgi->end_html;

Thanks again FishMonger!

FishMonger
03-02-2010, 05:02 PM
One error on my part.
$cgi->end_html;

Should be:
print $cgi->end_html;

However, since the module is basically a wrapper to 'tail -f', that print statement won't be executed. A production level script of this nature should have a reasonable timeout setup to close that connection and finish off the html i.e., send the closing body/html tags.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum