premal has asked for the wisdom of the Perl Monks concerning the following question:

Hi I have written a CGI Perl script which calls another Perl script (mytask.pl) using some parameters passed to it. Based on the parameters Mytask.pl decides which Operating System is used. In Mytask.pl has nested if-else for different tasks. It works fine for Windows and AIX using telent. For Linux I have used Net::SSH::W32Perl. When I run Mytask.pl from DOS command prompt and pass arguments manually it works fine but when I use CGI perl script it fails for the Module written for Linux. I have given the Linux module for the script below.

if ($ARGV[2] eq "Linux") { my $file = "C:\\Bounce\\$ARGV[0].txt"; open(INFO, ">$file"); print INFO "$ARGV[0]\n"; print INFO "$ARGV[3]\n"; print INFO "$ARGV[1]\n"; #Script executes fine upto this line. #From this line onwards it is not excuted and jumps to sleep (5) and e +xits; my $host = "$ARGV[0].domain.com"; my $user = "username"; my $pass = "password"; my $cmd = "cd $ARGV[3] && ./startServer.sh $ARGV[1]"; my $ssh = Net::SSH::W32Perl->new( $host, protocol => 2 ); $ssh->login($user, $pass); my($stdout, $stderr, $exit) = $ssh->cmd($cmd); #print $stdout, "\n"; print INFO $stdout; close(INFO); sleep (5); }

Replies are listed 'Best First'.
Re: Net::SSH::W32Perl is not working with CGI Perl script
by Anonymous Monk on Mar 22, 2012 at 10:21 UTC

    I have given the Linux module for the script below

    There doesn't appear anything implicitly wrong with the way you're calling ssh, meaning it should work if the host/user/pass is valid, but @ARGV isn't normally set under CGI

    So the problem could be that
    your webserver is running your perl cgi program under a user which doesn't have required permissions (limited account, not member of some group ... )
    your firewall is interrupting the perl program (or your webserver) from creating outgoing sockets
    connecting to a remote server is taking too long, and your webserver is killing your perl program before it is finished

    Solution? Check the logs, check the event viewer, check the firewall logs, get/give permissions or make member of group, change timeout or use Proc::Background and Watching long processes through CGI (Aug 02)

    But code like you posted is harder to debug/maintain/copy. Don't write code like that, write code like this

    So that when you run it, you get output like

    Then when trying to debug only the linux portion, you can write

    sub Main { MyLinuxRun( cdto => "ccddttoo", cmd => "cd ccddttoo && ./startServer.sh argyargyarg", debug => 1, host => "host", info => "refix/host.txt", pass => "pass", prefix => "refix", user => "user", ); }

    The free Modern Perl book cover many of these tips

    See also Devel::CheckOS, Getopt::Long, Data::Dump