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

In the following small script, I can not get $ssh->cmd executed successfully. $out and $err are all empty.

At the end of the debug log, it says: "
Authentication methods that can continue: password,publickey.
Next method to try is password.
Trying password authentication.
Login completed, opening dummy shell channel.
channel 0: new client-session
Requesting channel_open for channel 0.
channel 0: open confirm rwindow 131072 rmax 98304
channel 1: new client-session
Requesting channel_open for channel 1.
Channel open failure: 1: reason 2:"
What can possibly cause the error “Channel open failure: 1: reason 2”? Where should I look into?

==== my simple script:

use Net::SSH::Perl;
use Net::SSH::Perl::Cipher;
my $hostname = "****";
my $username = "****";
my $passwd = "****";
my $ssh = Net::SSH::Perl->new($hostname, port => 22, interactive=> 0, debug => 1, protocol => 2);
$ssh->login($username, $passwd);
my $cmd = "ls –l ";
my($out,$err) = $ssh->cmd($cmd);
print "the output is $out\n";

Replies are listed 'Best First'.
Re: Net::SSH::Perl question
by ig (Vicar) on Aug 03, 2009 at 23:17 UTC

    FWIW - your script works fine on my system: CentOS 5.3, perl v5.10.0, Net::SSH::Perl 1.34 and open-ssh 4.3p2-29.

    Reason 2 is SSH_OPEN_CONNECT_FAILED (see rfc4254). This is an error indication from the server back to the client. To find the cause of the failure you should investigate your ssh server. Maybe it is logging an error. Maybe you can run the server with debug option to get more information.

    update: There is something odd about your debug output. On my system I get:

    Login completed, opening dummy shell channel. channel 0: new [client-session] Requesting channel_open for channel 0. channel 0: open confirm rwindow 0 rmax 32768 Got channel open confirmation, requesting shell. Requesting service shell on channel 0. channel 1: new [client-session] Requesting channel_open for channel 1. Entering interactive session.

    The messages Login completed, opening dummy shell channel. and Got channel open confirmation, requesting shell. are both produced by the login method of Net::SSH::Perl::SSH2.pm. I have checked the source code on CPAN and the latter message is in every version back to 1.24 released back in 2003. Yet your trace doesn't have this second message. Recent versions have added a third argument to the login method to prevent opening a shell, but you have not used the third argument so the latter message should be there. I don't see any other path that would avoid the production of the latter message.

    This suggests that your version of Net::SSH::Perl is not as on CPAN. You may have a version modified by your distribution provider or you may have a corrupt version. You might try installing Net::SSH::Perl from CPAN and see what happens.

      Thanks for the reply.

      Below is the trace again. It looks just like yours, except the last line with the error message. (I guess I missed something when I pasted my trace first time.)

      Do you think it is a connection issue, or perl module issue?

      Login completed, opening dummy shell channel.
      channel 0: new client-session
      Requesting channel_open for channel 0.
      channel 0: open confirm rwindow 131072 rmax 98304
      Got channel open confirmation, requesting shell.
      Requesting service shell on channel 0.
      channel 1: new client-session
      Requesting channel_open for channel 1.
      Entering interactive session.
      Channel open failure: 1: reason 2:

        OK, that looks normal, except for the channel open failure. This is an error indication from the SSH server you are connecting to. You should investigate the server for clues, including any logs it produces. You should find an explanation there.

        What ssh server are you connecting to and what operating system is it running on?

        If you don't need a pseudo terminal (pty) you might try added use_pty => 0 to your options to Net::SSH::Perl->new(). There is a comment in the source code that support for pty is experimental. Maybe it doesn't work with some servers. You don't need a pseudo terminal to run an ls command.

Re: Net::SSH::Perl question
by Khen1950fx (Canon) on Aug 03, 2009 at 23:43 UTC
    It works great for me. I think that the problem is:

    my($out,$err) = $ssh->cmd($cmd); print "the output is $out\n"
    Hmmm...If you take a closer look at it, you'll see three mistakes. First, my($out,$err). $out and $err are being interpreted as filehandles here, and that's not what we want. Note that there isn't supposed to be a comma between $out and $err as filehandles. Also, you forgot $exit---without that, it won't work.

    my ( $stdout, $stderr, $exit ) = $ssh->cmd($cmd); works better.

    Second,  print "the output is $out\n";---$out is being interpreted as a global symbol, so you'll get warnings about needing an explicit package name. print $stdout, "\n"; eliminates that problem. Here's the complete script:

    #!/usr/bin/perl use strict; use warnings; use diagnostics; use Net::SSH::Perl; my $host = 'host'; my $user = 'user'; my $password = 'password'; my $cmd = 'ls -l'; my $ssh = Net::SSH::Perl->new( $host, port => 22, protocol => 2, debug => 1 ); $ssh->login( $user, $password ); my ( $stdout, $stderr, $exit ) = $ssh->cmd($cmd); print $stdout, "\n";
    Update: fixed typo
Re: Net::SSH::Perl question
by thezip (Vicar) on Aug 03, 2009 at 21:12 UTC
    I'm sure that you've checked the obvious, but I'll go ahead and mention it anyway.

    You have manually logged into the account with those credentials, yes?

    Is there a firewall blocking your access?

    What can be asserted without proof can be dismissed without proof. - Christopher Hitchens
      Yes, mannually log in works fine.
Re: Net::SSH::Perl question
by tokpela (Chaplain) on Aug 03, 2009 at 21:48 UTC

    If you just need support for SSH2 - try Net::SSH2

Re: Net::SSH::Perl question
by Illuminatus (Curate) on Aug 03, 2009 at 21:41 UTC
    Do you not have an ssh client on the machine executing the script? If you do, I would recommend switching to Net::SSH or Net::SSH::Expect. I have used the latter quite a bit, with no problems. Is the server system running OpenSSH? If so, what version?
Re: Net::SSH::Perl question
by FloydATC (Deacon) on Aug 03, 2009 at 21:46 UTC
    For what it's worth, I tried to use Net::SSH::Perl for some heavy lifting a few months ago and had to give it up for this exact reason. Never figured out exactly why it didn't work, I just assumed that the module was either incomplete or broken.

    Ended up using Net::SSH2 instead. Channel creation would still fail intermittently for no apparent reason, but atleast it worked well enough to get the job done.

    I hope it helps...

    -- Time flies when you don't know what you're doing