Home > Archive > PERL CGI Beginners > November 2006 > Script produces nothing when run from web page
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Script produces nothing when run from web page
|
|
| Richard Luckhurst 2006-10-30, 6:55 pm |
| Hi All
I have a simple perl cgi script that works fine and produces the correct output
when run from a command line. However when called as a cgi script from within a
web page the script does not run correctly.
The script is called log-errors and is called as follows
log-errors?domain=www.resmaster.com
The script is as follows
#!/usr/bin/perl
#
use strict;
use warnings;
use CGI qw/:standard -no_xhtml/;
my $domain = param('domain');
my $tail = `tail -n2000 /var/log/httpd/$domain-error_log 2>/dev/null | tac`;
$tail =~ s/</</g;
$tail =~ s/>/>/g;
print header;
print start_html (
-title => 'Exodus Web Server Log',
-lang => 'en-UK',
-link => 'blue',
-vlink => 'blue');
print font({size => 4}, b("Last 2000 Lines of the Error log for: $domain")), br, "\n";
print font({size => 1}, br), "\n";
print 'Go to ', a({href => '#bottom'}, i('End of log')), br, br, "\n";
print pre($tail), "\n";
print a({name => 'bottom'}), br, "\n";
print b('End of log'), br, "\n";
print end_html, "\n\n\n";
I have tried putting a print statement in to see if the $domain variable gets
set and it does get set to www.resmaster.com. The print line also uses the
$domain variable and the correct thing is printed on the html page.
The system command produces nothing, the $tail variable is empty, when run as a
cgi script. When I run the script from a command line (and I force $domain to be
www.resmaster.com as I can't pass it in) I find that the system call works and I
get the data I would expect. In fact I left the $domain set to www.resmaster.com
and tried running it as a cgi and I get nothing from the system call.
I am using apache 2.2 as my webserver and perl 5.8.8 both as they came with
Fedora core 5.
Can anyone offer any suggestions about why this does not work?
Regards
Richard Luckhurst
| |
| John Kennedy 2006-10-30, 6:55 pm |
| Hi ,
I am no expert but I would guess that cgi runs in a restricted environment
and doesn't have a full PATH, thus it doesn't know how to call tail and
tac.
I'd try putting the full path to these in the script.
/usr/bin/tail
/usr/bin/tac
instead of just tail and tac.
HTH, if not I am sure some guru will set you (and me) straight.
cheers
johnk
On Wed, 25 Oct 2006, Richard Luckhurst wrote:
> Hi All
>
> I have a simple perl cgi script that works fine and produces the correct output
> when run from a command line. However when called as a cgi script from within a
> web page the script does not run correctly.
>
> The script is called log-errors and is called as follows
>
> log-errors?domain=www.resmaster.com
>
> The script is as follows
>
> #!/usr/bin/perl
> #
>
> use strict;
> use warnings;
>
> use CGI qw/:standard -no_xhtml/;
>
> my $domain = param('domain');
> my $tail = `tail -n2000 /var/log/httpd/$domain-error_log 2>/dev/null | tac`;
> $tail =~ s/</</g;
> $tail =~ s/>/>/g;
>
> print header;
>
> print start_html (
> -title => 'Exodus Web Server Log',
> -lang => 'en-UK',
> -link => 'blue',
> -vlink => 'blue');
>
> print font({size => 4}, b("Last 2000 Lines of the Error log for: $domain")), br, "\n";
> print font({size => 1}, br), "\n";
> print 'Go to ', a({href => '#bottom'}, i('End of log')), br, br, "\n";
> print pre($tail), "\n";
> print a({name => 'bottom'}), br, "\n";
> print b('End of log'), br, "\n";
> print end_html, "\n\n\n";
>
>
> I have tried putting a print statement in to see if the $domain variable gets
> set and it does get set to www.resmaster.com. The print line also uses the
> $domain variable and the correct thing is printed on the html page.
>
> The system command produces nothing, the $tail variable is empty, when run as a
> cgi script. When I run the script from a command line (and I force $domain to be
> www.resmaster.com as I can't pass it in) I find that the system call works and I
> get the data I would expect. In fact I left the $domain set to www.resmaster.com
> and tried running it as a cgi and I get nothing from the system call.
>
> I am using apache 2.2 as my webserver and perl 5.8.8 both as they came with
> Fedora core 5.
>
> Can anyone offer any suggestions about why this does not work?
>
> Regards
>
> Richard Luckhurst
>
>
>
>
>
>
>
>
>
>
--
Dr. John A Kennedy email: John.Kennedy@cern.ch
LMU Muenchen
Sektion Physik
Am Coulombwall 1 Phone: 0049(89)2891 4152
D 85748 Garching, Germany Fax: 0049(89)2891 4103
| |
| Mumia W. 2006-10-30, 6:55 pm |
| On 10/25/2006 01:23 AM, Richard Luckhurst wrote:
> Hi All
>
> I have a simple perl cgi script that works fine and produces the correct output
> when run from a command line. However when called as a cgi script from within a
> web page the script does not run correctly.
>
> The script is called log-errors and is called as follows
>
> log-errors?domain=www.resmaster.com
>
> The script is as follows
>
> #!/usr/bin/perl
> #
>
> use strict;
> use warnings;
>
> use CGI qw/:standard -no_xhtml/;
>
> my $domain = param('domain');
> my $tail = `tail -n2000 /var/log/httpd/$domain-error_log 2>/dev/null | tac`;
Mr. Luckhurst, this is a disaster waiting to happen:
log-errors?domain=%3Bls%20%2F%3B
Perhaps you want to sanitize $domain before giving it to the shell:
$domain =~ s/[^a-z0-9_.-]//ig;
Or, better, you can abort if $domain is "improper":
die if ($domain =~ m/[^a-z0-9._-]/);
> $tail =~ s/</</g;
> $tail =~ s/>/>/g;
>
$tail = escapeHTML($tail);
As Mr. Kennedy said, it might be a PATH or permissions problem. Build
$tail up in pieces, testing only a little bit at a time. First, test if
you can use the tail command:
$tail = `tail /some/publicly/accessible/file`;
Then try it with the parameter:
$tail = `tail -n200 /some/publicly/accessible/file`;
Then try it with your log file:
$tail = `tail -n2000 /var/log/httpd/${domain}-error_log`;
And so on. You might also want to change the header() to 'text/plain'
for testing, because you want to see the raw data in $tail. In an HTML
file, a "<" without a corresponding ">" would make the rest of the file
invisible.
> print header;
>
> print start_html (
> -title => 'Exodus Web Server Log',
> -lang => 'en-UK',
> -link => 'blue',
> -vlink => 'blue');
>
> print font({size => 4}, b("Last 2000 Lines of the Error log for: $domain")), br, "\n";
> print font({size => 1}, br), "\n";
> print 'Go to ', a({href => '#bottom'}, i('End of log')), br, br, "\n";
> print pre($tail), "\n";
> print a({name => 'bottom'}), br, "\n";
> print b('End of log'), br, "\n";
> print end_html, "\n\n\n";
>
>
> I have tried putting a print statement in to see if the $domain variable gets
> set and it does get set to www.resmaster.com. The print line also uses the
> $domain variable and the correct thing is printed on the html page.
>
> The system command produces nothing, the $tail variable is empty, when run as a
> cgi script. When I run the script from a command line (and I force $domain to be
> www.resmaster.com as I can't pass it in) I find that the system call works and I
> get the data I would expect. In fact I left the $domain set to www.resmaster.com
> and tried running it as a cgi and I get nothing from the system call.
>
> I am using apache 2.2 as my webserver and perl 5.8.8 both as they came with
> Fedora core 5.
>
> Can anyone offer any suggestions about why this does not work?
>
> Regards
>
> Richard Luckhurst
>
>
>
>
>
>
>
>
>
Fedora Core has something called selinux, that restricts access. If
necessary, configure selinux to let apache cgi scripts have access to
files in /var/log/httpd/*
Good luck.
| |
| Todd W 2006-11-21, 9:55 pm |
|
"Richard Luckhurst" <rluckhurst@resmaster.com> wrote in message
news:1941990143.20061025162359@resmaster.com...
> Hi All
>
> I have a simple perl cgi script that works fine and produces the correct
> output
> when run from a command line. However when called as a cgi script from
> within a
> web page the script does not run correctly.
....
> use CGI qw/:standard -no_xhtml/;
....
> my $domain = param('domain');
....
> When I run the script from a command line (and I force $domain to be
> www.resmaster.com as I can't pass it in)...
Yes you can. Just use the query string as an argument to your program and
CGI.pm will parse the argument.
$ myprog.pl domain=www.example.com foo=bar
Todd W.
|
|
|
|
|