Aldebaran has asked for the wisdom of the Perl Monks concerning the following question:
I've been lurking on threads where there has been discussion about varied forms of secured communication: SSH, SSH2, sftp. It's nice to have source to refer to, but without explanations for some details, I find myself cargo-culting in methods I don't understand. One reads that SSH2 is experimental in perl, so naturally, I have to try it, and I'd like to ask a few questions.
The install for Net::SFTP::Foreign::Backend::Net_SSH2 was discussed in Resolving issues while installing Net::SSH2 on Ubuntu 16.04.2.
sudo apt install libssh2-1-dev sudo apt install openssh-server sudo apt install zlib1g-dev sudo cpan cpan[1]> install Net::SFTP::Foreign::Backend::Net_SSH2
Not much happened on the first and third command for me, but a lot seemed to happen on the second and ultimate.
Q1) I've been using Net::SFTP::Foreign for quite a while now without having this module. How typical is it for modules to have an "experimental backend"?
I then encountered A little demo for Net::SSH2 where runrig has made a somewhat recent update that says to change all instances of the code "$chan->blocking(0);" to "$chan->blocking(1)". As I've already confessed, I'm pasting a lot of this in, as I don't know what any form of blocking is. First I'll display output and then list source:
$ ./4.ssh2_1.pl ... drwx---r-t 29 u61210220 ftpusers 4096 Sep 16 22:54 . drwxr-xr-t 7 root root 4096 Sep 27 00:12 .. -rw------- 1 u61210220 ftpusers 3235 Sep 26 20:18 .bash_history drwx---r-x 6 u61210220 ftpusers 133 Sep 15 17:15 .cpan ... drwxr-xr-x 2 u61210220 ftpusers 61 Jun 9 2011 zen LINE : Linux infong972 3.16.0-ui18135.21-uiabi1-infong-amd64 #1 SMP De +bian 3.16.56-1~ui80+1 (2018-05-15) x86_64 GNU/Linux execution goes through here Can't call method "read" on an undefined value at ./4.ssh2_1.pl line 3 +8. $ cat 4.ssh2_1.pl #!/usr/bin/perl -w use 5.011; use Net::SFTP::Foreign; use Net::SSH2; use Data::Dumper; my $ssh2 = get_tiny_ssh2(); my $chan = $ssh2->channel(); $chan->blocking(1); $chan->exec('ls -la'); while (<$chan>) { print } $chan->close; #shell use my $chan2 = $ssh2->channel(); $chan2->blocking(1); $chan2->shell(); print $chan2 "uname -a\n"; print "LINE : $_" while <$chan2>; $chan2->close; say "execution goes through here"; my $chan3 = $ssh2->channel(); $chan3->blocking(1); $chan3->exec('pwd'); while (<$chan>) { print } $chan3->close; # my $sftp = Net::SFTP::Foreign->new( ssh2 => $ssh2, backend => 'Net_SSH2', timeout => '10' ) or die "Can't connect: $!\n"; # get a dirlist my $dir1 = "~/perlmonks/scripts"; my $dh = $sftp->opendir($dir1); while(my $item = $dh->read) { print $item->{'name'},"\n"; } undef $ssh2; sub get_tiny_ssh2 { use 5.011; use warnings; use Net::SFTP::Foreign; use Config::Tiny; use Data::Dumper; use Net::SSH2; my $ini_path = qw( /home/bob/Documents/html_template_data/3.values.i +ni ); say "ini path is $ini_path"; my $sub_hash = "my_sftp"; my $Config = Config::Tiny->new; $Config = Config::Tiny->read( $ini_path, 'utf8' ); say Dumper $Config; # -> is optional between brackets my $domain = $Config->{$sub_hash}{'domain'}; my $username = $Config->{$sub_hash}{'username'}; my $password = $Config->{$sub_hash}{'password'}; my $port = $Config->{$sub_hash}{'port'}; my $key_path = $Config->{$sub_hash}{'key_path'}; #dial up the server say "values are $domain $username $password $port $key_path"; my $ssh2 = Net::SSH2->new( timeout => '30000' ); $ssh2->connect( $domain, $port ); $ssh2->auth( username => $username, password => $password ); return $ssh2; } __END__ $
Execution seems to hang at the line that says
$chan2->close;The "who" command that some have used goes nowhere.
Q2) I've got 2 different timeout values hard-coded in this script. Is that a problem? What values are reasonable?
Q3) There is no output from opening the the third channel. Why?
Finally, when I try to do anything fancy in replicating runrig's development, I get an error that I'm reading an undefined value. The $dir1 doesn't look non-existent to me, but I can't get pwd to verify that yet.
So, I'm fishing for fixes and explanations. Thank you for comment.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: using SSH2 backend
by salva (Canon) on Oct 01, 2018 at 09:20 UTC | |
by Aldebaran (Curate) on Oct 02, 2018 at 01:09 UTC | |
|
Re: using SSH2 backend
by zentara (Cardinal) on Sep 28, 2018 at 12:06 UTC | |
by salva (Canon) on Oct 01, 2018 at 09:17 UTC | |
by Aldebaran (Curate) on Oct 01, 2018 at 23:39 UTC |