Solved

Need to log cgi form output

Posted on 2004-04-22
18
209 Views
Last Modified: 2013-12-25
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
Comment
Question by:mcabot
  • 10
  • 8
18 Comments
 
LVL 3

Expert Comment

by:cjmos
ID: 10895292
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
 
LVL 3

Expert Comment

by:cjmos
ID: 10895306
oops (not that it matters)

#timestamp
$date = gmtime;
0
 
LVL 3

Expert Comment

by:cjmos
ID: 10895325
Also,

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



0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:mcabot
ID: 10896165
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
 
LVL 3

Expert Comment

by:cjmos
ID: 10898858
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
 

Author Comment

by:mcabot
ID: 10900216
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
 
LVL 3

Expert Comment

by:cjmos
ID: 10900887
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
 
LVL 3

Expert Comment

by:cjmos
ID: 10901201
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
 

Author Comment

by:mcabot
ID: 10901336
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
 

Author Comment

by:mcabot
ID: 10901374
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
 

Author Comment

by:mcabot
ID: 10901580
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
 
LVL 3

Expert Comment

by:cjmos
ID: 10901738
Between each line or between each entry? should only be one line between each entry.
0
 

Author Comment

by:mcabot
ID: 10901873
cjmos, between each line, there are two to three spaces. I would like none.


0
 
LVL 3

Expert Comment

by:cjmos
ID: 10902061
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
 

Author Comment

by:mcabot
ID: 10902615
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
 
LVL 3

Accepted Solution

by:
cjmos earned 500 total points
ID: 10902780
Change to:

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


should be fine now.
0
 

Author Comment

by:mcabot
ID: 10902883
cjmos, PERFECT!!! Thank you for all your help.
0
 
LVL 3

Expert Comment

by:cjmos
ID: 10902977
No probs.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question