http://www.perlmonks.org?node_id=1220031

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

I'm trying to get a Net::SSH2 program working and I'm running into a snag. The code seems to be straight from the manual and is dead simple;

$ssh2->connect(HOST) 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 ; print "never get here\n" ;

It zips through connecting and authenticating and then just hangs there -- the channel() never returns. I haven't a clue what I've done wrong...

Replies are listed 'Best First'.
Re: Net:SSH2 channels
by zentara (Archbishop) on Aug 07, 2018 at 20:54 UTC
Re: Net:SSH2 channels
by wjw (Priest) on Aug 07, 2018 at 21:13 UTC

    . 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...

      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

      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!
Re: Net:SSH2 channels
by salva (Canon) on Aug 08, 2018 at 10:49 UTC
    Ensure you are using the latest version of the module and of libssh2. Then, don't follow any advice about using the module more than a couple of years old (It used to have lots of bugs needing ugly work arounds which are now fixed). Though, using the module is still not easy.

    Also, try using it through Net::SSH::Any which provides a much friendly interface and knows how to handle most errors.

    libssh2 has a debugging mode that you can activate, but the library needs to be compiled with it enabled