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

First off, I know that this is a Perl forum and this really is a Perl question. So, here goes: I have a PHP web page that calls a Perl script that I have written using Net::SSH::Perl. I have fully tested this and it works just fine from the cli. But, it does not complete when I call it from PHP. The pertinent code segment that is having trouble is this:
my %params = ( protocol => "2", debug => "true", use_pty => 0 ); print "entering Net:SSH:Perl->new command\n"; $ssh = Net::SSH::Perl->new($serverName, %params); print "entering SSH->login command\n"; $ssh->login($user, $pass); print "executing cmd\n"; ($out,$err,$exit) = $ssh->cmd("/home/bfdi533/pna_switch.pl $pna_swid $ +pna_mode"); print "*****\n"; print "results:\n$out\n";
Here is the output on the web page:
entering Net:SSH:Perl->new command entering SSH->login command
Should I be calling this code the way I am? Should I be trying to write this using mod_perl rather than calling the perl script from a PHP system command? Any and all help and/or advice would be appreciated.

Replies are listed 'Best First'.
Re: Net::SSH::Perl trouble from withing PHP
by dws (Chancellor) on Jan 07, 2005 at 20:27 UTC

    I have fully tested this and it works just fine from the cli. But, it does not complete when I call it from PHP.

    If you don't provide a identity_files key, Net::SSH::Perl tries to find one via $ENV{HOME}/.ssh/known_hosts. Assuming you're using PHP with Apache, the apache user probably doesn't have a .ssh directory.

    Try using identity_files, and make sure any files you reference are readable by the apache user. You might also try adding interactive => 0 (explained in the Net::SSH::Perl docs).

    This can also be a problem with non-PHP CGIs.

      OK, so here is where I am now. The apache user does not have a home directory so I have:
      1. created a directory, /var/www/localhost/htdocs/switchdb/.ssh
      2. I added id_dsa and id_rsa from my own user .ssh directory
      3. created an identity file with the connection data again extracted from my .ssh directory.
      4. changed owner to apache with 700 perms on directory an all sub-files
      5. modified the code so it now looks like this
      my %params = ( protocol => "2", debug => "true", use_pty => 0, interac +tive => "false", identity_files => ("/var/www/localhost/htdocs/switch +db/.ssh/identity") );
      I am now getting further in my code but get the following:
      Starting run_pna_switch.pl processing request for pna_swid 17 and mode on processing request for pna_swid 17 and mode on entering Net:SSH:Perl->new command entering SSH->login command The authenticity of host '146.6.167.199' can't be established. Key fingerprint is 99:80:ac:53:50:14:5f:9a:04:ce:5f:44:0f:5c:7d:39. Are you sure you want to continue connecting (yes/no)? [yes]

      So, of course I tried to run the code as 'su - apache /usr/bin/perl code.pl' but it does not run and does not produce any output and does not ask me the question. I am stumped here as to why this code won't run for me as apache on the cli to get this interactive question answered.

      Actually, nothing that I run as 'su - apache' works including 'su - apache ssh 1.2.3.4' to try to do this interactively from the cli.

      Ed

        created a directory, /var/www/localhost/htdocs/switchdb/.ssh

        Educated speculation: Try making the connection by hand (using ssh), then find and copy the line added to your ~/.ssh/known_hosts to /var/www/localhost/htdocs/switchdb/.ssh/known_hosts (creating the file if you need to), then point entity_files to that.

      Thanks for the pointer. I had read this in the man page but did not really stop to think that apache runs my code rather than my user.

      I will add this and give it a try.

      Ed

Re: Net::SSH::Perl trouble from withing PHP
by xorl (Deacon) on Jan 07, 2005 at 20:36 UTC

    If it works fine from the command line but doesn't work when PHP calls it, I suspect this is more of a PHP question.

    Off hand I don't see anything wrong with the perl part of this.

    You might want to check out:
    http://pecl.php.net/package/ssh2

    It looks like it should give PHP some more functionality.

      I think it's less of a PHP question per se, but rather a generic inter-process server environment issue, along the lines of dws's suggestion above.