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

Hello Monks,

I have been experimenting with ssh in the past and I know to apply this simple task that I am asking. But since I was told and start reading that ssh2 is a more secure way to communicate through hosts I thought it would be a good idea to give it a try.

I have created a sample script based on the module documentation Net::SSH2::Channel.

Sample of code provided bellow:

#!/usr/bin/perl use strict; use warnings; use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect('hostname', 22) or $ssh2->die_with_error; my $chan = $ssh2->channel() or $ssh2->die_with_error; $chan->blocking(0); $chan->exec("ls ./") or $ssh2->die_with_error; while (<$chan>){ print } print "exit status: " . $chan->exit_status . "\n"; $ssh2->disconnect();

Unfortunately for me, I am doing something wrong and the script stays hanged until it dies with the following error:

no libssh2 error registered at /home/tinyos/perl5/lib/perl5/x86_64-lin +ux-gnu-thread-multi/Net/SSH2.pm line 54.

I tried to debug it by removing the channel and print part and having only the connect and disconnect steps. It seems to be working fine just like that but not been able to execute the commands.

I am using Linux 4.4.0-42-generic x86_64 if this makes a difference.

I know that I could execute an ssh session through perl like:

my @output = `ssh -p 22 username "ls"`; chomp @output; print Dumper \@output;

Since I am using ssh-keys I have no problem but just for fun I would like to learn how to use Net::SSH2 in case I want to implement a script been able to executed in Windows as well.

Update: After a lot of research online I can not find a solution. It seems that I have installed all the necessary packages but I assume that there is a bug on libssh2 package. I can not verify it 100% but it seems like that.

Thank you all for your time and effort in advance, I will be online again in a few hours.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re: How to execute and capture output of remote commands using Net::SSH2
by salva (Canon) on Oct 13, 2016 at 08:48 UTC
    Which version of libssh2 are you using? For proper error handling you need version 1.7.0.

    Anyway, I have uploaded a new version of the module (0.63) that now generates errors that would point you to the line in your script where they happened... even if they don't tell you the cause!

    Finally, don't disable blocking mode unless you know what you are doing.

      Hello Salva

      You are absolutely right, I am running libssh2-1:amd64                               1.5.0-2                                       amd64        SSH2 client-side library this is why I can not see a proper error code.

      Unfortunately this is the version that cane with the latest openssh-server/client on my distribution. I also know that on Ubuntu libssh2 has 3 active bugs, see libssh2 package in Ubuntu .

      Most likely the solution to my problem would be to install libssh2 1.7.0-1 as you suggest. Thank you for your time and effort reading and replying to my question.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
        In Linux, usually, Net::OpenSSH is easier to use and doesn't need any dependency besides the binary ssh client.