• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Need to log cgi form output

I need the below script modified to use "remote_user" to gather the logged in user executing the script user name (we use apache htaccess) logged to a file. I also need the input and out put ($display) of the sumitted form logged to the same file, with a time and date stamp.  I don't think this is a very hard request but I need it done fast (500 points). Thanks in advance

#!/usr/bin/perl -w

# URL
my $me = "http://somwhere/private-cgi/operator/userid.pl";
my ($command, $result, $hidden, $userid, $server_instance, $display);

use strict;
use CGI;
# use CGI::Pretty;
# use CGI::Carp qw(fatalsToBrowser);

my $q = new CGI;

if ($q->param('userid'))  {
     $userid = $q->param('userid');
     if ($userid =~ /^([a-zA-Z\d_\s]+)$/ )  {$userid = $1 }
     else  { error($userid) }

     $hidden = './oneuser.sh';

     my $server_instance  = $q->param('server_instance');
     if ($server_instance =~ /^([a-zA-Z\d_\s]+)$/ )  {$server_instance = $1 }
     else  { error($server_instance) }

     $command = "$hidden $userid $server_instance";
     $result = `$command`;
     #$display = "Command = $command<br>Results = $result";
     $display =  $result;

}

else  {

     $display = 'Welcome to Houston User Setup!!'
}

# begin webpage

print      $q->header(-type=>'text/html'),
$q->start_html(     -title=>'Houston User Setup',
               -bgcolor=>'#ffffff',
               -onLoad=>'Javascript:document.houst.userid.select()'),
$q->start_form( -name=>'houst',
               -method=>'post',
               -action=>$me),
$q->table( {   -align=>'center',
               -border=>0,
               -width=>400,
               -cellspacing=>10 },
$q->Tr( [
$q->td( { -align=>'center'},
[ $q->font({-face=>'arial', -size=>'+2'}, $q->b('Houston User Setup'))]),
$q->td( { -align=>'center'}, [ $q->hr]),
$q->td( [ $q->font ({-face=>'arial', -size=>'+0'}, $q->b('User ID:'))]),
$q->td( [ $q->textfield(-name=>'userid', -size=>'20', -default=>'') ]),

# actual argument passed to the server
$q->td( [ $q->popup_menu(-name =>'server_instance',
-values =>['duane_sblprod','duane_sblqa','duane_sbluat','duane_sbltrain','cash_f
idev','cash_sbltest'],

# labels on webpage
-labels => {duane_sblprod => 'Prod', duane_sblqa => 'Qa', duane_sbluat => 'Uat',
 duane_sbltrain => 'Training', cash_fidev => 'Dev', cash_sbltest => 'Test'}) ]),
$q->td( [ $q->submit (-value=>'Submit') ] ),
$q->td( [ $q->font({-face=>'arial', -size=>'+0'}, $q->b('Output:'))]),
$q->td( { -bgcolor=>'#cccccc' }, [ $display ]),
               ])
          ),
$q->end_form,
$q->end_html;

sub error {
  my $text = shift;

   print  $q->header(-type=>'text/plain');
   print "$text Only letters and numbers are permitted.";
   exit;
0
mcabot
Asked:
mcabot
  • 10
  • 8
1 Solution
 
cjmosCommented:
Hi, to print the log:

$username=$ENV{REMOTE_USER};
#timestamp
$input - What ever the input is
$date = gmtime;


open (LOG,">logname") or die "Can't create log files $!";

print LOG "USER: $username
               INPUT: $input
               OUTPUT: $display
               $date";

close LOG;
0
 
cjmosCommented:
oops (not that it matters)

#timestamp
$date = gmtime;
0
 
cjmosCommented:
Also,

gmtime - universal standard time.
localtime(time()) - local time.



0
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
mcabotAuthor Commented:
Would you please put it in the script, so I could just copy and paste the script from here back to the server. I have very little experience with cgi. Thank you.
0
 
cjmosCommented:
mcabot, I don't know what you wanted to call your log file, in the below i've kept it simple and just called it "log-username.txt". If the the log file already exists then it is called "log-username(1).txt",  "log-username(2).txt". etc

There was also a '}' missing from the end of your error sub which i added to my example.


#!/usr/bin/perl -w

# URL
my $me = "http://somwhere/private-cgi/operator/userid.pl";
my ($command, $result, $hidden, $userid, $server_instance, $display);

use strict;
use CGI;
# use CGI::Pretty;
# use CGI::Carp qw(fatalsToBrowser);

my $q = new CGI;

if ($q->param('userid'))  {
     $userid = $q->param('userid');
     if ($userid =~ /^([a-zA-Z\d_\s]+)$/ )  {$userid = $1 }
     else  { error($userid) }

     $hidden = './oneuser.sh';

     my $server_instance  = $q->param('server_instance');
     if ($server_instance =~ /^([a-zA-Z\d_\s]+)$/ )  {$server_instance = $1 }
     else  { error($server_instance) }

     $command = "$hidden $userid $server_instance";
     $result = `$command`;
     #$display = "Command = $command<br>Results = $result";
     $display =  $result;

}

else  {

     $display = 'Welcome to Houston User Setup!!'
}

# begin webpage

print      $q->header(-type=>'text/html'),
$q->start_html(     -title=>'Houston User Setup',
               -bgcolor=>'#ffffff',
               -onLoad=>'Javascript:document.houst.userid.select()'),
$q->start_form( -name=>'houst',
               -method=>'post',
               -action=>$me),
$q->table( {   -align=>'center',
               -border=>0,
               -width=>400,
               -cellspacing=>10 },
$q->Tr( [
$q->td( { -align=>'center'},
[ $q->font({-face=>'arial', -size=>'+2'}, $q->b('Houston User Setup'))]),
$q->td( { -align=>'center'}, [ $q->hr]),
$q->td( [ $q->font ({-face=>'arial', -size=>'+0'}, $q->b('User ID:'))]),
$q->td( [ $q->textfield(-name=>'userid', -size=>'20', -default=>'') ]),

# actual argument passed to the server
$q->td( [ $q->popup_menu(-name =>'server_instance',
-values =>['duane_sblprod','duane_sblqa','duane_sbluat','duane_sbltrain','cash_f
idev','cash_sbltest'],

# labels on webpage
-labels => {duane_sblprod => 'Prod', duane_sblqa => 'Qa', duane_sbluat => 'Uat',
 duane_sbltrain => 'Training', cash_fidev => 'Dev', cash_sbltest => 'Test'}) ]),
$q->td( [ $q->submit (-value=>'Submit') ] ),
$q->td( [ $q->font({-face=>'arial', -size=>'+0'}, $q->b('Output:'))]),
$q->td( { -bgcolor=>'#cccccc' }, [ $display ]),
               ])
          ),
$q->end_form,
$q->end_html;

############
##Start new code
my $username=$ENV{REMOTE_USER};
#timestamp
my $date = localtime(time);
my $logfile="log-$username.txt";

while (-e $logfile){
                my $i=1;
      $logfile="log-$username($i).txt";
      $i++;
}

open (LOG,">$logfile") or die "Can't create $logfile - $!";

print LOG "USER: $username
OUTPUT: $display
$date";

close LOG;
##End new code
###########

sub error {
  my $text = shift;

   print  $q->header(-type=>'text/plain');
   print "$text Only letters and numbers are permitted.";
   exit;
}       ### <- This was missing from your posted version
0
 
mcabotAuthor Commented:
cjmos, thank you for your help, but it's not quite right. The log needs to capture all the input and results each time the script is executed. Right now it is capturing the userid fine (I like that the logfile is titled with the users id), but it is doing a few things incorrectly.

The user is going to login once, and create many userid's. All of the input and results need to be captured in the same log file. There may be several people logging into the page to create id's, they each need their own log file. Right now the log is only capturing the first login, the "OUTPUT" is printing "Welcome to Houston user setup!!", and each time the script is executed the log is being over written. The input and results from each execution of the script need to be appended to the log file. The point of the log, is that I need to have an audit trail of every userid setup, and which user created it.
0
 
cjmosCommented:
Gotcha, wasn't sure before about how you wanted it to work. OK, simple enough...

replace my original code with the following:

############
##Start new code
my $username=$ENV{REMOTE_USER};
#timestamp
my $date = localtime(time);
my $logfile="log-$username.txt";

open (LOG,">>$logfile") or die "Can't create $logfile - $!";      #>> to append to file

print LOG "USER: $username
ID CREATED: $userid
OUTPUT: $display
$date\n\n";

close LOG;
##End new code
###########
0
 
cjmosCommented:
Also, put the line

$userid = "NONE";

above the line

$display = 'Welcome to Houston User Setup!!'

so:

else  {
     $userid = "NONE";
     $display = 'Welcome to Houston User Setup!!'
}

This is just to avoid any warnings (Which you probably won't even notice) when the script is run for the first time and $userid has no value;
0
 
mcabotAuthor Commented:
cjmos, still having problems. Below is what I find in the log file, and nothing is being printed to the log file after the first time a log in and execute the script.

USER: mcabot
ID CREATED: NONE
OUTPUT: Welcome to Houston User Setup!!
Fri Apr 23 11:11:32 2004

~
~
~
~
~
~
~
~
~
~
~
~
USER: mcabot
ID CREATED: NONE
OUTPUT: Welcome to Houston User Setup!!
RESULTS:
Fri Apr 23 11:13:20 2004

~
~
~
~
~
~
~
          ),
$q->end_form,
$q->end_html;

my $username=$ENV{REMOTE_USER};
#timestamp
my $date = localtime(time);
my $logfile="log-$username.txt";

open (LOG,">>$logfile") or die "Can't create $logfile - $!";      #>> to append
to file

print LOG "USER: $username
ID CREATED: $userid
OUTPUT: $display
$date\n\n";

USER: mcabot
ID CREATED: NONE
OUTPUT: Welcome to Houston User Setup!!
Fri Apr 23 11:14:36 2004
0
 
mcabotAuthor Commented:
Sorry about the last post. The only thing being printed to the log file is:

USER: mcabot
ID CREATED: NONE
OUTPUT: Welcome to Houston User Setup!!
Fri Apr 23 11:11:32 2004
0
 
mcabotAuthor Commented:
cjmos, disregard my last two posts, it works. I only have one remaining issue. The log file has alot of spaces in between each line. Is there anyway we can print the log with out all the spaces?
0
 
cjmosCommented:
Between each line or between each entry? should only be one line between each entry.
0
 
mcabotAuthor Commented:
cjmos, between each line, there are two to three spaces. I would like none.


0
 
cjmosCommented:
That's weird, i can't think why they'd be spaces between the lines.

can i take a look at one of your logs? just want to see if i can spot why it's happening.
0
 
mcabotAuthor Commented:
cjmos, if you look below I modified the script just a little. I wanted the date to be first in the log. It is but, there is two spaces between it and the next line. I also wanted the return of $command

# this is the two spaces in the log that I want to get rid of.
(Fri Apr 23 13:27:09 2004


USER: mcabot)


Changes I made:

print LOG "$date\n\n # moved date to here
USER: $username
COMMAND: $command
OUTPUT: $display";
0
 
cjmosCommented:
Change to:

print LOG "$date
USER: $username
COMMAND: $command
OUTPUT: $display\n\n";      #\n means new line


should be fine now.
0
 
mcabotAuthor Commented:
cjmos, PERFECT!!! Thank you for all your help.
0
 
cjmosCommented:
No probs.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now