in reply to Re: Net:SSH2 channels
in thread Net:SSH2 channels

I'm now truly confused. Every example, here and in other places, has no apparent problem getting a channel. And I can't get even that to work. When I run this:

my $ssh2 = Net::SSH2->new() ; $ssh2->connect(HOST) or $ssh2->die_with_error ; $ssh2->auth_password($login{user}, $login{password}) ; $ssh2->auth_ok() ; my $chan = $ssh2->channel() or $ssh2->die_with_error ; print "Got a channel\n" ;

"got a channel" never gets printed and my program is just dead in the water. I discovered that if I wait long enough {a few minutes} I do get it to exit:

d:\Perl>sshlogin.pl Connecting Connected Logged in no libssh2 error registered at D:\Perl\sshlogin.pl line 31.
OK, I just tried it again, but timing it this time -- it took right around two minutes and gave me the non-error error. Do I need to give some parameters or something to the original 'connect' or the like?

Hmmm.. I just tweaked the program to try logging into a different SSH/shell account I have and the same thing happens. It just won't set up a connection. Very bizarre!

Replies are listed 'Best First'.
Re^3: Net:SSH2 channels
by syphilis (Archbishop) on Aug 08, 2018 at 02:02 UTC
    $ssh2->auth_password($login{user}, $login{password});

    We first need to establish exactly where you're script is failing.
    I suspect that authorization might be the problem.
    Instead do:
    $ssh2->auth_password($login{user}, $login{password}) or die "Auth failed";
    $ssh2->auth_ok();

    The value returned by that expression will tell you whether you are authorized or not - but you haven't checked that value.
    You'd normally check that value:
    $ssh2->auth_ok() or die "Not authorized";
    On Srawberry Perl, I'm finding that auth_password won't work now that I've got public key authorization set up. (Not sure if that's "just me", or "just windows", or "just the way it's supposed to be".)
    So, if the user you're connecting as has public key authorization set up, you might need to authorize using auth_publickey. Here's the script I'm using with Strawberry Perl 5.28.0 on Windows 7, to channel into a local Ubuntu box:
    use warnings; use Net::SSH2; use strict; my @output; my $ssh = Net::SSH2->new(); $ssh->connect("host") or die "Unable to connect host\n"; # auth_password no longer working for me # $ssh->auth_password("user", "password") # or die "Failed to auth\n"; # using auth_publickey works $ssh->auth_publickey( "user", "C:\\cygwin\\home\\user\\.ssh\\id_rsa.pub", "C:\\cygwin\\home\\user\\.ssh\\id_rsa", 0) or die "Auth failed"; die "Not authenticated" unless $ssh->auth_ok; # Double checking my $channel = $ssh->channel() or die "Channel creation failed\n"; $channel->blocking(1); $channel->exec("ls ~"); while (<$channel>) {push @output, $_} print scalar @output, "\n"; # See no. of entries print for @output;
    Works fine for me - but doesn't necessarily help you ;-)

    UPDATE: I also have SSH access to a remote machine via the internet, and this access requires auth_password authorization (as public key authorization has not been set up).
    The above script also works fine for it, once I switch from using auth_publickey to auth_password.

    Cheers,
    Rob

      Perfect suggestion!! In the code I had

      $ssh2->auth_password($login{user}, $login{password}) ; $ssh2->auth_ok() ;
      and I was assuming that auth_ok was telling me truth. Not so. As you suggested, I put in an explicit error check on the auth_password and now I get:
      d:\Desktop>sshtest Authentication failed (username/password) (-18 LIBSSH2_ERROR_PUBLICKEY +_UNRECOGNI ZED) at D:\Desktop\sshtest.pl line 13.
      Now I have to figure out what PUBLICKEY_UNRECOGNIZED means. I think it means that it doesn't recognize the servers key and I need to figure out how to get that "accepted" -- I know I've had to do that in my sftp and ssh clients, I didn't think it'd be a problem with this. Is it worth a bug-report on Net::SSH2 that auth_ok doesn't seem to work?
        Now I have to figure out what PUBLICKEY_UNRECOGNIZED means

        According to the documentation it means that "The username/public key combination was invalid".
        It's therefore a rather strange error - because you weren't even providing a "username/public key" combination.
        Instead, you were providing a "username/password" combination.

        However, that's the exact same error as I, too, experience when I try to authorize using auth_password. (As I mentioned in my previous post, I experience no problems if I seek authorization with auth_publickey.)
        It has a very buggy feel about it, but I'm not sure what the bug is or whether it has been fixed in a more recent version of Net::SSH2 (I have version 0.63) or libssh2 (I have version 1.8.0).

        Cheers,
        Rob