Easy One I'm Sure.


D

Degz

Hi,

I'm just learning how to use perl with CGI, and have come up against a
problem whereby I can't seem to get multiple lines back to my web page.


My prog looks like this

#!/usr/bin/perl -wT

use strict;
use CGI;

my $cgi_mod = new CGI;

# Find Command And Execute It.
#my $command = $cgi_mod->param( 'COMMAND' );
my $command = "ls -l";
my @ret_vals = `$command`;

print $cgi_mod->header( "text/html" ),
$cgi_mod->start_html( -title => "These Are The Arguments I
Received.",
-bgcolor => "#ffffff",
-style => "text/css",
-color => "black");
print $cgi_mod->h1("Please See Below Output Of The Command You
Typed."),
$cgi_mod->hr;

foreach my $line_by_line (@ret_vals) {
print $cgi_mod->p("$line_by_line")
}

print $cgi_mod->end_html;

The problem is the print statement in the foreach loop only seems to
print the first line of my data. If however I run the perl script on
it's own I get all data returned ???

Any help is appreciated.
Cheers
Degz
 
Ad

Advertisements

A

A. Sinan Unur

@g14g2000cwa.googlegroups.com:


Subject: Easy One I'm Sure.

Please choose a meaningful subject line. For tips on how to compose an
effective post, please read the posting guidelines for this group.
I'm just learning how to use perl with CGI, and have come up against a
problem whereby I can't seem to get multiple lines back to my web page.

What does that mean?
My prog looks like this

Please fully spell words: "program" not "prog".

#!/usr/bin/perl -wT

use strict;
use CGI;

my $cgi_mod = new CGI;

my $cgi = CGI->new;

is better style, IMHO. No unnecessary typing, and avoids indirect object
notation.
# Find Command And Execute It.
#my $command = $cgi_mod->param( 'COMMAND' );

You should not blindly accept commands to be run on the server from web
clients. What if param('COMMAND') is 'rm -rF /'?
my $command = "ls -l";
my @ret_vals = `$command`;


This is misleading.

my @output = `$command`;
print $cgi_mod->header( "text/html" ),
$cgi_mod->start_html( -title => "These Are The Arguments I
Received.",
-bgcolor => "#ffffff",
-style => "text/css",
-color => "black");
print $cgi_mod->h1("Please See Below Output Of The Command You
Typed."),
$cgi_mod->hr;

foreach my $line_by_line (@ret_vals) {
print $cgi_mod->p("$line_by_line")

perldoc -q always
}

print $cgi_mod->end_html;


Personally, I hate generating HTML via CGI.pm. But, even then, this is
particularly ugly and hard to read. There is also a lot of extraneous
code that is not related to your problem.

I do not observe the behavior you are complaining about. You might want
to test the following code:

#!/usr/bin/perl -T

use strict;
use warnings;

$| = 1;

use CGI;

my $cgi = CGI->new;

print $cgi->header,
$cgi->start_html,
$cgi->p([ `ls -r` ]),
$cgi->end_html;
__END__

Sinan
 
J

J. Gleixner

Degz said:
Hi,

I'm just learning how to use perl with CGI, and have come up against a
problem whereby I can't seem to get multiple lines back to my web page.


My prog looks like this

#!/usr/bin/perl -wT

use strict;
use CGI;

my $cgi_mod = new CGI;

my $cgi_mod = CGI->new;
# Find Command And Execute It.
#my $command = $cgi_mod->param( 'COMMAND' );
my $command = "ls -l";
my @ret_vals = `$command`;

Hopefully you're not going to let someone type in a command and this
script will execute it.
print $cgi_mod->header( "text/html" ),
$cgi_mod->start_html( -title => "These Are The Arguments I
Received.",
-bgcolor => "#ffffff",
-style => "text/css",

This isn't the correct value for 'style'.
-color => "black");

Avoid using 'black'. If you're going to set it, use '#000000', just
like you used '#ffffff' above.
print $cgi_mod->h1("Please See Below Output Of The Command You
Typed."),
$cgi_mod->hr;

Why have separate print statements for these? You can just add them to
the above print.
foreach my $line_by_line (@ret_vals) {
print $cgi_mod->p("$line_by_line")

No need to double quote that value.

print $cgi_mod->p( $line_by_line );
}

print $cgi_mod->end_html;

The problem is the print statement in the foreach loop only seems to
print the first line of my data. If however I run the perl script on
it's own I get all data returned ???

Running this dies with "Insecure $ENV{PATH} while running with -T
switch". After fixing that, the value for '-style' gave me a warning.
After fixing that, it seems to display the output. Maybe more helpful
information is listed in your error log.
 
D

Degz

Hi,

Many Thanks for that. No I'm not going to allow anyone to run commands
in a live environment. I'm mearly trying to get a feel for passing data
and getting output back.

Thanks for all the input. I will take it on board. Can I ask how you
fixed the "Insecure $ENV{PATH}" and also the '-style' warning please?

Degz.
 
Ad

Advertisements

A

Anno Siegel

A. Sinan Unur said:
@g14g2000cwa.googlegroups.com:

Subject: Easy One I'm Sure.

Please choose a meaningful subject line. For tips on how to compose an
effective post, please read the posting guidelines for this group.
[...]
# Find Command And Execute It.
#my $command = $cgi_mod->param( 'COMMAND' );

You should not blindly accept commands to be run on the server from web
clients. What if param('COMMAND') is 'rm -rF /'?

Nothing much, on my system. "rm -rf", however...

Anno
 
Ad

Advertisements


Top