in reply to Net:SSH2 channels

. I gave this a shot in my environment with positive results. This is on a Linux box running v5.22.1

Most of the code is from the Synopsis on MCPAN. I tried this connecting to both a regular Linux box and a Raspberry Pi. The Pi does not have keys set up on it, but the Linux box does. I thought perhaps the auth method might be a problem when a uname/passwd combo is handed to a machine which is expecting a ssh key. But that is apparently not a problem.

Anyway, the code itself is tested and functions in my environment so, that may give you a known starting point.

Hope that is somehow helpful...

Edit:

I have not used Net::SSH previously, so this is a learning experience for me. Zentara's post is a good one. Also, there is this node which provides some really good examples to try -> A little demo for Net::SSH2

#!/usr/bin/perl use Modern::Perl; #removed no strict refs from here after originally posting.... use Net::SSH2; my ( %login, $login); my $ssh2 = Net::SSH2->new(); $login = %login; $login{'user'}='***'; $login{'password'} ='*******'; $ssh2->connect('192.168.0.15') or $ssh2->die_with_error ; $ssh2->auth_password($login{user}, $login{password}) ; $ssh2->auth_ok() ; #### print "Logged in\n" ; #### my $chan = $ssh2->channel() or ssh2->die_with_error ; my $cmd = $chan->exec('ls'); my $sftp = $ssh2->sftp(); #### my $fh = $sftp->open('.bashrc') or $sftp->die_with_error; say $_ while <$fh>; $ssh2->disconnect();

...the majority is always wrong, and always the last to know about it...

A solution is nothing more than a clearly stated problem...

Replies are listed 'Best First'.
Re^2: Net:SSH2 channels
by BernieC (Pilgrim) on Aug 07, 2018 at 23:42 UTC

    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!

      $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?
Re^2: Net:SSH2 channels
by Anonymous Monk on Aug 08, 2018 at 02:05 UTC

    I copied your code exactly, only changed the host, user and password, and I'm, again, greeted with

    d:\Desktop>sshtest Logged in Can't locate object method "die_with_error" via package "ssh2" (perhap +s you forg ot to load "ssh2"?) at D:\Desktop\sshtest.pl line 17.
    it even duplicated my typo in the original {forgot a $ in the ssh->die... line} Something strange is going on and I'm not sure how to chase it down. I guess I have to grit my teeth and try reading some of the code in Net::SSH2::Channel and perhaps sticking in some debugging info... Ugh!