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!

In reply to How to execute and capture output of remote commands using Net::SSH2 by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.