LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   CGI script to view log files (https://www.linuxquestions.org/questions/programming-9/cgi-script-to-view-log-files-533698/)

twantrd 03-01-2007 05:03 PM

CGI script to view log files
 
I'm trying to get a basic perl script to view some log files over the web for some users. For starters, I have this:

Code:

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<html><title>Log File Viewer</title><body>";
print "<h1><center><font color=white>Log File Viewer</h1></center></font>";
print "<body bgcolor=black>";
print "<TABLE border=1><td>";
#my $tail=`tail -100 /var/log/twantrd.messages`;
open(LOG, "/var/log/twantrd.messages") or die "Can't open file $!";
print <LOG>;
close(LOG);
#print "<font color=red>$tail</font>";
print "</td></TABLE>";
print "</body></html>";

The contents of the log does display however all the lines wrap around making it unreadable. Is there a way to display all the carriage returns of the log file via html? Thanks.

-twantrd

theNbomr 03-01-2007 07:08 PM

Print <PRE> tags around your log file:

Code:

print "<PRE>\n";
print <LOG>;
print "</PRE>\n";

--- rod

twantrd 03-02-2007 02:59 AM

Ahhh, thanks!

I have one more question. I'm trying to setup a simple cgi script in perl that will scp some files when it's being called on via the web. For example, http://serverA/cgi-bin/scp.pl, will then run my perl script. The code is simple:

Code:

#!/usr/bin/perl

print "Content-type: text/html\n\n";
system("/usr/bin/scp user@host:/data/log.file /tmp/log.file");

However, nothing prints on screen and my file never gets scp'd over. BUT, if I wrote this in a bash script, it works just fine. What's wrong with my perl syntax?

-twantrd

theNbomr 03-02-2007 10:11 AM

I'm a bit confused about that last post. The link that you posted seems to point to a holding URL that is for sale. That aside, are you saying that you have one script calling another script? Also, I'm confused about your reference to running a bash script and seeing output on your console. If you run a script that writes to standard output as a CGI, the standard output goes to the HTTP socket that is opened by the HTTP server. That's the whole essence of how CGI works. There are some differences between the contexts of running any script, bash, perl, whatever, as an ordinary user from the commandline, and as the user that the web server runs as in the CGI context. One difference is the permissions normally assigned to the cgi-bin directory. As a CGI, your script is trying to write to the cgi-bin directory, which the web server should not have write access to (a fundamental security issue).
Please clarify what you are seeing (or not seeing), in both the test and production situations.

--- rod.

theNbomr 03-02-2007 10:19 AM

Okay, re-examining your question, I think I see what you may be questioning. You are printing the first line of the HTTP header, but the rest of the HTML is missing. You should probably create a proper HTML page with the <html> <head></head> <body> </body> </html> components. Some confirmation message in the body would be good, too.
I assume you have arranged ssh keys to permit paswordless secure copies.
--- rod.


All times are GMT -5. The time now is 09:30 AM.