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.
|
|---|
| 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 | |
by thanos1983 (Parson) on Oct 26, 2016 at 15:16 UTC | |
by salva (Canon) on Oct 26, 2016 at 19:32 UTC |