Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Pass output from shell script back to perl

Reply
Thread Tools

Pass output from shell script back to perl

 
 
sitnam81
Guest
Posts: n/a
 
      04-28-2005
Hello all,

I am working on a web front end that would allow administrators to
manage Solaris users on multiple servers -- a php page with a form
passes input to this perl cgi script, which ssh's to the selected
servers and executes shell scripts to add/remove the input user. These
schell scripts return one line of output, which I want to pass back to
the perl script, to display the results. I keep getting "500" errors
with the following in the apache log:

[error] malformed header from script. Bad header=user inputusername
removed: add_remove_user.cgi, referer: http://IP/useradmin.php

Like the example above the output from the shell script is like "user
inputusername removed" -- how can I pass this back to the page? Here
is the perl scipt:

************************************************** *****************
#!/usr/bin/perl -w

print "Content-type:text/html\n\n";

$loop = 0;
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

if($value eq "ON") {
$SERVER{$loop} = $name;
$loop++
}
else {
$FORM{$name} = $value;
}
}

$loopc = $loop;
$user = "specifieduser";
$addcommand = "sudo /etc/scripts/useradd $FORM{uname} $FORM{UID}
$FORM{group}";
$rmcommand = "sudo /etc/scripts/userremove $FORM{uname}";

print "<html><head><title>Form Output</title></head><body>";
print "<h2>Results from FORM post</h2>\n";

if($FORM{pass} eq "") {
print "Please enter a password<br>";
print "<br><a href='javascript: history.go(-1)'>Back</a>";
}

elsif($FORM{pass} eq $FORM{pass2}) {
if($FORM{radio} eq "add") {
for($loop = 0; $loop < $loopc; $loop++)
{
print "ADDING Username: $FORM{uname} UID: $FORM{UID} Group:
$FORM{group} to $SERVER{$loop}\n\n";
#$ADDRESPONSE = system("ssh", "-l", "$user", "-q", "$SERVER{$loop}",
$addcommand);
#print "$ADDRESPONSE\n";
print system("ssh", "-l", "$user", "-q",
"$SERVER{$loop}", $addcommand);
}
}
else {
for($loop = 0; $loop < $loopc; $loop++)
{
print "REMOVING Username: $FORM{uname} UID: $FORM{UID}
Group: $FORM{group} to $SERVER{$loop}\n\n";
#system("ssh", "-l", $user, "-q", $SERVER{$loop},
$rmcommand, "| 2>&1 >/dev/null");
$RMRESPONSE = system("ssh", "-l", "$user", "-q",
"$SERVER{$loop}", $rmcommand);
print "$RMRESPONSE\n";
print system("ssh", "-l", "$user", "-q",
"$SERVER{$loop}", $rmcommand);
}
}

print "<br><a href='javascript: history.go(-1)'>Back</a>";
}

else {
print "Passwords do not match<br>";
print "<br><a href='javascript: history.go(-1)'>Back</a>";
}

print "</body></html>";

************************************************** *****************

Thanks!

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-28-2005
"sitnam81" <> wrote in
news: oups.com:

> [error] malformed header from script. Bad header=user inputusername
> removed: add_remove_user.cgi, referer: http://IP/useradmin.php


Well, you are not sending the correct headers to the browser.

> ************************************************** *****************
> #!/usr/bin/perl -w


use warnings;

is better because it allows you to selectively turn warnings on and off.

You also need:

use strict;

> print "Content-type:text/html\n\n";


Are you sure that is correct?

> $loop = 0;
> read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
> @pairs = split(/&/, $buffer);
> foreach $pair (@pairs) {
> ($name, $value) = split(/=/, $pair);
> $value =~ tr/+/ /;
> $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;


Please do not use buggy home-brewed solutions for parsing CGI data.

use CGI;

perldoc CGI

should get you started.

Sinan

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-28-2005
sitnam81 wrote:
> I am working on a web front end that would allow administrators to
> manage Solaris users on multiple servers -- a php page with a form
> passes input to this perl cgi script, which ssh's to the selected
> servers and executes shell scripts to add/remove the input user. These
> schell scripts return one line of output, which I want to pass back to
> the perl script, to display the results. I keep getting "500" errors
> with the following in the apache log:
>
> [error] malformed header from script. Bad header=user inputusername
> removed: add_remove_user.cgi, referer: http://IP/useradmin.php


I don't know why you get that error. You shouldn't, since you have:

> print "Content-type:text/html\n\n";


at the beginning of the script as well as several other print statements
before any system() command is executed.

<snip>

> print system("ssh", "-l", "$user", "-q", "$SERVER{$loop}", $addcommand);


Nevertheless, you'd better study

perldoc -f system

and read about what you should use instead to capture the output.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-28-2005
sitnam81 <> wrote:


> I keep getting "500" errors



So you have already seen the answer to your Frequently Asked Question then?

perldoc -q 500

My CGI script runs from the command line but not the browser. (500
Server Error)


> read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
> @pairs = split(/&/, $buffer);
> foreach $pair (@pairs) {
> ($name, $value) = split(/=/, $pair);
> $value =~ tr/+/ /;
> $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
>
> if($value eq "ON") {
> $SERVER{$loop} = $name;
> $loop++
> }
> else {
> $FORM{$name} = $value;
> }
> }



If you insist on decoding forms yourself, then you are on your own.

perldoc -q form

How do I decode a CGI form?



> print system("ssh", "-l", "$user", "-q",



If you read the documentation for the function that you are
using you will see both what system() returns (which you
are then print()ing) and how to capture the output from
external programs.

This is not a "read the docs to me" service.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
sitnam81
Guest
Posts: n/a
 
      04-29-2005
Well I am able to test all components successfully:
1.) The ssh + shell script component successfully returns the desired
result
2.) The perl works fine -- loads in the form data and correctly calls
the desired script
3.) If I remove the output of the script, the page returns fine after
submitting

Only when I allow the script to return results does it cause the 500
error. This leads me to believe that it is not a webserver problem
because it can handle the perl and the originating php page. Therefore
I believe it is a problem with the way the perl script handles the
output from the shell script.

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-29-2005
sitnam81 <> wrote:

> I believe it is a problem with the way the perl script handles the
> output from the shell script.



They way you are calling the shell script, it is not a problem with
Perl.

The output goes to wherever the output normally goes, often STDOUT
(which would be inherited from the perl process).

Where do you _want_ the STDOUT from the shell script to go?


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
sitnam81
Guest
Posts: n/a
 
      04-29-2005
I would like the output from the shell script to be fed back into the
perl script.
Then i would like it to include that output in the resulting html for
the "submit" page.

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-29-2005

[ Please quote some context in followups like everyone else does. ]


sitnam81 <> wrote:

> I would like the output from the shell script to be fed back into the
> perl script.



Then I have already answered your question in an earlier followup.

Did you follow the advice I gave? It contains the answer to your question!


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-29-2005
"sitnam81" <> wrote in
news: ups.com:

Please quote some context.

> I would like the output from the shell script to be fed back into the
> perl script.


>>> print system("ssh", "-l", "$user", "-q", "$SERVER{$loop}",
>>> $addcommand);


Then why are you using system?

Do you know what system returns?

Have you read

perldoc -f system

Sinan

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-29-2005
Tad McClellan wrote:
> sitnam81 wrote:
>> I would like the output from the shell script to be fed back into the
>> perl script.

>
> Then I have already answered your question in an earlier followup.


Are you sure of that? If you are, I'm curious. How can the server
generate the error mentioned in the original post when there are several
valid print statements before the system() call, including a
content-type header?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to pass shell variable to shell script from python D'Arcy J.M. Cain Python 0 02-27-2008 01:56 PM
Re: How to pass shell variable to shell script from python Gerardo Herzig Python 1 02-27-2008 12:19 PM
Re: How to pass shell variable to shell script from python Christian Heimes Python 0 02-27-2008 10:53 AM
execute a shell script in a shell script moongeegee Perl Misc 2 12-04-2007 12:18 AM
perl script to pass data to another perl script? PhEaSaNt PLuCKeR Perl Misc 1 10-30-2005 07:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57