Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    CGI "tail" script somewhat working

    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?

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <PRE>tail --lines=60 -f /var/log/maillog</PRE>
    nothing shows up at all.

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    Can you post your script so I can see exactly what you're doing?

  • #5
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.
    Attached Files Attached Files

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    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/Fil...0.99.3/Tail.pm

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

  • #7
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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"

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    Please post your Perl script and we'll see what we can figure out.

  • #9
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here it is:

    Code:
    #!/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
    Last edited by supanatral; 03-02-2010 at 03:46 PM.

  • #10
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    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.
    Code:
    #!/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;

  • #11
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

  • #12
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    You could try this:
    Code:
    while ( my $line = $file->read ) {
        print $cgi->pre($line);
    }

  • #13
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #14
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,942
    Thanks
    2
    Thanked 169 Times in 164 Posts
    What's wrong with the formatting?

    Is everything displaying as a single line? If so, then we simply need to add a <br> tag.
    Code:
    print $cgi->escapeHTML($line), $cgi->br;
    Or, you could do this:
    Code:
    print $cgi->pre( $cgi->escapeHTML($line) );

  • Users who have thanked FishMonger for this post:

    supanatral (03-02-2010)

  • #15
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:

    Code:
    #!/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!
    Last edited by supanatral; 03-02-2010 at 04:57 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •