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

Hi All,

  I'm desperately trying to get this working. I've come across some strange behavior, which I noticed someone else has mentioned in the modules comments. I have written a simple script that works great from the command prompt, but it drops out when I try and run it in the browser. I've done a couple of tests and can't figure why...

print "Content-type: text/html\n\n"; print "1-"; use Net::SSH::Perl; print "2-"; my $ssh = Net::SSH::Perl->new($Config->{ssh}->{host}, {protocol => 2, +debug => true}); print "3-"; $ssh->login($Config->{ssh}->{user}, $Config->{ssh}->{pass}); print "4-"; my ($stdout, $stderr, $exit) = $ssh->cmd("ls"); print "Output:- $stdout, $stderr, $exit";
Ran from command prompt this works great (returns 1-2-3-4-Output, etc...). Ran in browser is drops out (returns 1-2-3-). Apache is set to use SuEXEC so the script is executed as user "cosmic", so I changed to user cosmic from the command prompt and tried it and it still worked. So it appears that it only will not work when ran through browser... I have no idea why, and I'm getting no errors, even with the debug option set. When using top to monitor the process, from the command prompt it takes about 24 seconds to connect, and it the browser it stops after only 12 seconds. I know apache is set to let the process run for 300 seconds so it's not killing the process (or at least shouldn't be).

I'm lost... pls help.

Lyle Hopkins

Update:- Get the same problem if I use wget to retrieve the the scripts output.

Update:- Found the problem and have got it working now. The problem is that the $ENV{'home'} variable is used for saving ssh info, and that isn't available through CGI. I've done an update to get the users home folder and it's all working now. I've emailed the modules maintainer with an update and fix.

Replies are listed 'Best First'.
Re: Net::SSH:Perl problems when ran as CGI
by MidLifeXis (Monsignor) on Aug 30, 2007 at 18:23 UTC

    I am not familiar with this, but perhaps you need a PTY?

    My understanding of applications of this type (ssh) is that they require, by default, you to run them from a terminal. Extra parameters may be required to get past this.

    If this helps, great, otherwise ignore the node :)

    --MidLifeXis

Re: Net::SSH:Perl problems when ran as CGI
by runrig (Abbot) on Aug 30, 2007 at 18:36 UTC
    What is the output (probably in the error logs) with the debug option set? You could even write your own debug message (to STDERR) when debugging is turned on (instead of printing as you are to STDOUT) with Net::SSH::Perl's debug method.
      Which error log do you think it'll be in? Are we talking apache error logs, or something else? How would I write my own debug message, as soon the program gets to the login phase it just dies.

      Just had a thought.. Does apache block CGI scripts from using certain ports? Although the first connection line is working so it should have already connected to the host before it logs on? I really have no idea what to do.
Re: Net::SSH:Perl problems when ran as CGI
by runrig (Abbot) on Aug 30, 2007 at 18:52 UTC
    my $ssh = Net::SSH::Perl->new($Config->{ssh}->{host}, {protocol => 2, debug => true});

    Something tells me you aren't using use strict;. I would recommend using it. It may not fix your current problem, but it may make future problems easier to debug.

Re: Net::SSH:Perl problems when ran as CGI
by juanmatias (Initiate) on Sep 07, 2007 at 15:17 UTC
    It would be great to have that update. I'm having the same problem using Apache/CGI/SSH/SSH-Agent/keychain(Gentoo)/Perl. Please, post it. Thanks a lot.
      could you please help me, I am having similar problem when I run as CGI it does not work. Thank you, SH
Re: Net::SSH:Perl problems when ran as CGI
by jiraf (Initiate) on Sep 06, 2007 at 19:04 UTC
    Could you post the update here ? I'm running into the same problem.