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

OK, here's a strange problem..

My code can connect to localhost just fine, and behaves as expected.

Yet, anything but localhost doesn't work and errors out in the way I'm showing below.

Any ideas? (and yes, the login/password combo is correct, and I can manually connect with ssh using the same login credentials as I'm trying here)

--------------------------

#!/usr/bin/perl use warnings; use strict; use Net::SSH2; use Data::Dumper; my $hostname = shift; chomp($hostname); my $ssh2 = Net::SSH2->new(); $ssh2->connect($hostname) or die "cant connect to $hostname: $@ \n"; $ssh2->auth_password('root','mypassword') or die "cant login to $hostn +ame: $@ \n"; my $chan = $ssh2->channel(); $chan->blocking(0); $chan->shell(); print $chan "uname -a\n"; my @uname = <$chan>; print @uname; print $chan "who\n"; my @who = <$chan>; print @who; $chan->close;
[root@lpo-wiki-01 ~]# ./ssh.pl localhost Linux lpo-wiki-01 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:02 EDT 2007 i6 +86 i686 i386 GNU/Linux root pts/0 2008-05-15 07:54 (val.vmsinfo.com) root pts/1 2008-05-15 12:11 (val.vmsinfo.com) root pts/2 2008-05-18 11:42 (10.41.0.213) [root@lpo-wiki-01 ~]# ./ssh.pl yoda Use of uninitialized value in concatenation (.) or string at ./ssh.pl +line 11. cant connect to yoda: [root@lpo-wiki-01 ~]#

Replies are listed 'Best First'.
Re: Net::SSH2 strangeness
by apl (Monsignor) on May 18, 2008 at 17:44 UTC
    $@ is the error returned by an eval. I think you want to display $!, the error returned by almost anything else. (Please note that no error is displayed after the name of the host you're connecting to.)

    Make this change, run it again, and you'll have a better idea what the problem is.

    (Sorry, I don't know why the connect is complaining about a bad concatenation, although I'm certain someone else will be able to answer that.)

      And $! is for system calls. $ssh2->connect is not a system call, so $! is not appropriate either. Net::SSH2 provides the error method to get error messages. (Although it looks like it croaks for some errors from looking at the source.)

      (Sorry, I don't know why the connect is complaining about a bad concatenation, although I'm certain someone else will be able to answer that.)
      That's not connect, that's $@ being undef in "cant connect to $hostname: $@ \n".
Re: Net::SSH2 strangeness
by vxp (Pilgrim) on May 18, 2008 at 17:44 UTC

    Some more details: code is running on rhel 5, all boxes i'm going to be connecting to will also be rhel (2.1 3 4 and 5). there are no firewall rules preventing ssh connections from the box this code runs to any of the other boxes.

    also, just tried using ssh2->error() but it seems to be useless.

    -----------------

    #!/usr/bin/perl use warnings; use strict; use Net::SSH2; use Data::Dumper; my $hostname = shift; chomp($hostname); my $ssh2 = Net::SSH2->new(); $ssh2->connect($hostname) or die "cant connect to $hostname\n"; $ssh2->auth_password('root','mypassword') or die "cant login to $hostn +ame\n"; (my $code, my $error_name, my $error_string) = $ssh2->error(); if ($code) { print "$code: $error_name: $error_string \n"; exit(); } my $chan = $ssh2->channel(); $chan->blocking(0); $chan->shell(); print $chan "uname -a\n"; my @uname = <$chan>; print @uname; print $chan "who\n"; my @who = <$chan>; print @who; $chan->close;
    [root@lpo-wiki-01 ~]# ./ssh.pl localhost Linux lpo-wiki-01 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:02 EDT 2007 i6 +86 i686 i386 GNU/Linux root pts/0 2008-05-15 07:54 (val.vmsinfo.com) root pts/1 2008-05-15 12:11 (val.vmsinfo.com) root pts/2 2008-05-18 11:42 (10.41.0.213) [root@lpo-wiki-01 ~]# ./ssh.pl yoda cant connect to yoda [root@lpo-wiki-01 ~]#

      also, just tried using ssh2->error() but it seems to be useless.

      You die before actually reaching that code. Something like the following would be needed:

      $ssh2->connect($hostname) or do { my ($err_code, $err_name, $err_text) = $ssh2->error(); die "cant connect to $hostname: " . "($err_code, $err_name) $err_text\n"; };
        Now we are getting somewhere:
        [root@lpo-wiki-01 ~]# ./ssh.pl yoda cant connect to yoda: (-5, LIBSSH2_ERROR_KEX_FAILURE) Unable to exchan +ge encryption keys [root@lpo-wiki-01 ~]#

        wtf? :)