Can HTML do this?

gkainz

Final Approach
Joined
Feb 23, 2005
Messages
8,401
Location
Arvada, CO
Display Name

Display name:
Greg Kainz
I want to make some Unix log files visible via a browser. I can't change anything in the Apache httpd.conf file since I'm piggy-backing on an apps server that's got other purposes (does that make me a parasite or a virus?) :)

First, without making an entry in the .conf file to allow a directory to be browseable, is there any way to dynamically list the contents of the directory as clickable links?

Second, is there any way to convert the unix LF to windoze displayable CR/LF via href command or other trick on the fly, so the logfile displays correctly? Currently it opens in Windoze notepad and doesn't handle the new line correctly.

Thanks.
 
You can't use a script?

It could be done with a one line perl or PHP script
 
I'm a DBA, not a coder ... perl does exist on this box - perhaps I could slip it in unnoticed. Got an example? :)
 
I'm a DBA, not a coder ... perl does exist on this box - perhaps I could slip it in unnoticed. Got an example? :)

Click on the source code link toward the bottom of the page here:
http://www.perl.com/pub/a/2000/11/begperl3.html

It's more than you need but you'll see how it's done.

The previous chapters give some perl basics. Coverting LFs is a trivial exercise you'll see in the early chapters

Put your .pl file in the www/cgi directory and it should work when called as

Code:
http://www.server.com/cgi/myscript.pl

...provided CGI is not disabled on the web server.

As long as you don't take ANY input from the user you shouldn't have any security concerns. Once you allow a client to give you anything and work on that you have to get concerned with someone using that for no good.

Now Jesse will post it in a PHP one liner. :rolleyes:
 
Last edited:
Thanks!!! I'll have to check if cgi is disabled on this Apache server ... I do see a mod_perl that's enabled...

no input needed, required or allowed from the user - it's a non-published url that browses the results of database status report log files. I did find that if I changed the logfile extension from .log to .txt that the browser handles the unix line feeds correctly.
 
Thanks!!! I'll have to check if cgi is disabled on this Apache server ... I do see a mod_perl that's enabled...

no input needed, required or allowed from the user - it's a non-published url that browses the results of database status report log files. I did find that if I changed the logfile extension from .log to .txt that the browser handles the unix line feeds correctly.

Sorry! That sample doesn't HTMLize the output.

Here's how that works:
http://www.lies.com/begperl/hello_cgi.html
 
Now Jesse will post it in a PHP one liner. :rolleyes:

As per your request. Of course this isn't one line but it works.

The folder structure I tested this under was:

web directory: /var/www
log files located in: /var/www/logs
script placed: /var/www/view.php

You may modify it as you see fit. The key thing is I am assuming these log files are in the directory that the script is being ran from. You could write a cron script to copy them here every few minutes

Code:
<?php
//Put the name of the directory that resides where this script is being executed here.  The directory must be within the
//web path.  For example if your web directory is /var/www/ this file must be /var/www/view.php and the logs must be in
// /var/www/logs.  The below "logs" entry fits the above situation.
$directory = "logs";

//In order to open the directory we must know the absolute path.  These will establish the absolute path for us.
$scriptpath = realpath(dirname($_SERVER['SCRIPT_FILENAME']));
$diropen = opendir("$scriptpath/$directory");

//Now we loop through the directory contents and generate links to the logs.  In order for this to work properly these logs
//must be in the script's directory and must be web accessable.  This HTML isn't exactly W3C compliant but I am lazy and
//it works in firefox and internet exploder.

echo("<center>Files:<br><br>");
while($item = readdir($diropen)){
    if(($item != ".") && ($item != "..")){
        $files[] = $item;
        echo("<a href=\"$directory/$item\">$item</font></a><br />");
    }
}
?>
Attached are screen shots of the result.

I was just thinking. Apache will write clickable links of the files in a directory. So if the logs file is an accessable directory you just need Indexing on. You might be able to just drop a file called .htaccess in the logs directory and put in
Code:
Options +Indexes
Which will tell apache to index that directory if no file is specified. Of course this would require htaccess be enabled in the apache config file. If it is--you're golden. If not do what I wrote above.
 

Attachments

  • Screenshot-1.png
    Screenshot-1.png
    52.6 KB · Views: 9
  • Screenshot-2.png
    Screenshot-2.png
    47.2 KB · Views: 4
Last edited:
I think I can *almost* tell you how your program should look:

Code:
#!/usr/local/bin/perl

# replace the filename below with the path/name of your log file
$logfile="/var/www/log/db.txt"

open (LOG, "$logfile") or die "Could not open log $logfile - $!";

# Start the HTML page
print "Content-type: text/html\n\n";

print <<"EOF";
<HTML>

<HEAD>
<TITLE>DB Logs</TITLE>
</HEAD>

<BODY>
<H1>Log stats:</H1>
EOF



# print each line of the logfile until the end of the file
while ($line = <LOG>) 
{

# if you need to fix LFs do it here with $line=tr/010/013/...whatever
print $line "<BR>";

}

# close out the HTML 
print <<"EOF2"

</BODY>
</HTML>
EOF2
 
Last edited:
The key question: Are these log files going to be in the same directory as the script? If so--no need to be opening them and printing them. Just view them in the web browser and use a script to provide you with an index as I did above with php.

IF these web files are going to be somewhere else, then we can write something to open them and print them, BUT--Apache will still need access.
 
symbolic link in the Apache accessible directory pointing to the logfile dir takes care of the dir issue ...

just for quick 'n dirty I just spooled the dir listing into an .html file, tweaked the output to add the href and now all the logfiles are clickable ... which is good enough for today .

thanks for the thought and input - I'll play with these options on another apps server
 
symbolic link in the Apache accessible directory pointing to the logfile dir takes care of the dir issue ...

just for quick 'n dirty I just spooled the dir listing into an .html file, tweaked the output to add the href and now all the logfiles are clickable ... which is good enough for today .

thanks for the thought and input - I'll play with these options on another apps server

Like I said. A good chance if you just make a file called ".htaccess" and put in:
Code:
Options +Indexes

and look at the directory in your web browser, Apache will index the files for you in HTML output with links.
 
Back
Top