///Log Script Use

Log Script Use

Log Script Use

How many old, unused (or under-used) CGI scripts do you have lingering on your server, taking up space?

You may not be as guilty of abandoning CGI software in lieu of newer versions as we are (we’ve been creating, and testing software on the same server for 8 years) but odds are you’ve got a few dusty, unused scripts lurking about.

The question we run into is "Are we using this script for anything, and if we remove it, will it leave some form or other function adrift or broken?"

Thus today’s copy and paste code to log script use was written. We share it with you.

The are a number of good reasons for knowing when your CGI scripts are used on your server. It can help identify script hijacking attempts. You can know what is abandoned and what is attached to which form on what page. And you will be likely to notice what scripts need upgrading, too.

The copy ‘n paste code can be inserted into most Perl scripts, causing the scripts to append a record of information to a log file every time they run.

First, here is the copy ‘n paste code. Then, I’ll talk about it.

{ my $file  = "file.txt";

my @time = localtime;
$time[4]++;
$time[5] += 1900;
open Wlog,">$file" unless open Wlog,">>$file";
print Wlog "Year: $time[5] ";
print Wlog "Month: $time[4] ";
print Wlog "Day: $time[3] ";
print Wlog "Time: $time[2]:$time[1]:$time[0]";
print Wlog " — IP: $ENV{REMOTE_ADDR}";
print Wlog " — Referrer: $ENV{REFERER}";
print Wlog " — Self: $0\n";
close Wlog; }

Note that the first line of the code begins with an opening curly brace character, and that the last line of the code ends with a closing curly brace character. This has the effect of isolating the code’s variables from the variables in the rest of the script, negating the chance of variable name clashes.

Adding the code to your existing CGI scripts:

Always make a backup copy of any script before modifying it.

Putting the copy ‘n paste code immediately below the first line of the file should work for most scripts.

As usual, do all Perl script edits with a plain text word processor like NotePad or TextWrangler. FTP file transfers must likewise be as plain text.

Once the modified script has been uploaded, run it to verify the log file is created.

If no log file is created when the script runs, your server might not allow CGI scripts to create files in the directory where the script is running. In that case:

  1. Create a subdirectory for the log file, giving it correct permissions. (Try 755 first, then 766, and finally 777.)

  2. Change the first line of the copy ‘n paste code, replacing "file.txt" with the subdirectory name and the file name. Example: "log/file.txt"

The script should now create a log file (providing the subdirectory permissions are correct.)

When the script runs, it tries to open the log file. If unsuccessful, it creates the file.

Next, it grabs the server time and adjusts the month and year numbers.

Then, it appends a record to the file, one line per record.

The year, month, day, and clock time are first in the record. Next, the IP address, referrer (if available), and the location of the script itself.

Last, the file is closed.

The referrer information can help identify the form(s) that use the script. Not all browsers will provide referrer information, but many will.

When you view the log file with your FTP program, the file’s date and time will let you know the last time the script was used.

Opening the file will provide you with a record of each script launch.

2010-05-25T23:11:01+00:00 July 25th, 2005|CGI and Perl|0 Comments

About the Author:

Leave A Comment