#################################################### blocking ( flag ) Enable or disable blocking. A good number of the methods in Net::SSH2/libssh2 can not work in non-blocking mode. Some of them may just forcibly enable blocking during its execution. A few may even corrupt the SSH session or crash the program. The ones that can be safely called are read and, with some caveats, write. See "write" in Net::SSH2::Channel. Don't hesitate to report any bug you found in that area!####################################################
So........ don't feel lost, almost everyone is confused on this issue. :-)
UPDATE AUG 8, 2018: Revisited again, and please change all instances of the code "$chan->blocking(0);" to "$chan->blocking(1)". Newest version of the module will give error messages about blocking(0) being a no no.
UPDATE AUG 28 2007: Revisited this, and added code to prevent blocking on channel or shell use. Also note that a single channel can only exec 1 command, so use shell for multiple commands. Also attempts to create multiple channels or sftp objects will fail. Tested with the latest libs, openssl-0.9.8e , openssh-4.6p1, libssh2-0.17, Net-SSH2-0.10
From the Net::SSH2 README
Net::SSH2 is a perl interface to the libssh2 (http://www.libssh2.org) library. It supports the SSH2 protocol (there is no support for SSH1) with all of the key exchanges, ciphers, and compression of libssh2. At present, libssh2 requires OpenSSL (http://www.openssl.org) and can optionally use zlib for compression (http://www.zlib.net). The Net::SSH::Perl modules are showing signs of age; with the requirement to be compatible with SSH versions 1 and 2, support of newer features such as channels is difficult; it also has many dependencies, some of which are hard to build (e.g. Math::Pari).
So to help you get into it, here is a simple demo of what works, including a key authorization example. Some of the docs are hard to deal with because of way xs works, so here are some working usages that should allow you to handle almost anything, sftp, scp, running scripts, etc.
#!/usr/bin/perl use warnings; use strict; use Net::SSH2; use Data::Dumper; # see maillist archives at # http://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users my $ssh2 = Net::SSH2->new(); $ssh2->connect('localhost') or die "Unable to connect Host $@ \n"; #this works for passwords #$ssh2->auth_password('z','ztester') or die "Unable to login $@ \n"; #do key authorization like commandline shell # ssh -o PreferredAuthentications=publickey localhost # See: http://cfm.gs.washington.edu/security/ssh/client-pkauth/ # for setting up the keys # this dosn't work #$ssh2->auth(username=>'z', interact => 1); # so get the password for the key use Term::ReadKey; print "And your key password: "; ReadMode('noecho'); chomp(my $pass = ReadLine(0)); ReadMode('restore'); print "\n"; # works when run from z's homedir because you need # permission to read the keys $ssh2->auth_publickey('z', '/home/z/.ssh/id_dsa.pub', '/home/z/.ssh/id_dsa', $pass ); #works my $sftp = $ssh2->sftp(); my $fh = $sftp->open('/etc/passwd') or die; print $_ while <$fh>; #my $chan = $ssh2->channel(); #$chan->blocking(0); #$chan->exec('ls -la'); #while (<$chan>){ print } # to run a remote command in the background # you need to semi-daemonize it by redirecting it's filehandles my $chan3 = $ssh2->channel(); $chan3->blocking(1); $chan3->exec("nohup /home/zentara/perlplay/net/zzsleep > foo.out 2> fo +o.err < /dev/null &"); $chan3->send_eof; #to run multiple commands use a shell #shell use my $chan = $ssh2->channel(); $chan->blocking(0); $chan->shell(); print $chan "ls -la\n"; print "LINE : $_" while <$chan>; print $chan "who\n"; print "LINE : $_" while <$chan>; print $chan "date\n"; print "LINE : $_" while <$chan>; # file and directory operations #will get dir named 2 #my $chan1 = $ssh2->channel(); #$chan1->exec('ls -la 2'); #while (<$chan1>){ print } # mkdir with sftp #my $sftp = $ssh2->sftp(); my $dir = '/home/z/3'; $sftp->mkdir($dir); my %stat = $sftp->stat($dir); print Dumper([\%stat]), "\n"; #put a file my $remote = "$dir/".time; $ssh2->scp_put($0, $remote); #get a file #use IO::Scalar; #my $check = IO::Scalar->new; #$ssh2->scp_get($remote, $check); #print "$check\n\n"; # get a dirlist my $dir1 = '/home/z'; my $dh = $sftp->opendir($dir1); while(my $item = $dh->read) { print $item->{'name'},"\n"; } #shell use my $chan2 = $ssh2->channel(); $chan2->blocking(0); $chan2->shell(); print $chan2 "uname -a\n"; print "LINE : $_" while <$chan2>; print $chan2 "who\n"; print "LINE : $_" while <$chan2>; $chan2->close; __END__
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: A little demo for Net::SSH2
by runrig (Abbot) on Aug 10, 2010 at 15:10 UTC | |
by Always Improving (Initiate) on Oct 15, 2010 at 18:27 UTC | |
by zentara (Archbishop) on Oct 15, 2010 at 20:03 UTC | |
by Anonymous Monk on Oct 29, 2010 at 18:14 UTC | |
by zentara (Archbishop) on Oct 30, 2010 at 10:33 UTC | |
by afoken (Chancellor) on Oct 30, 2010 at 11:37 UTC | |
| |
Re: A little demo for Net::SSH2
by ryber (Acolyte) on Jan 13, 2011 at 23:49 UTC | |
by zentara (Archbishop) on Jan 14, 2011 at 16:26 UTC | |
Re: A little demo for Net::SSH2
by Anonymous Monk on Dec 04, 2007 at 23:48 UTC | |
by Tux (Canon) on Sep 10, 2008 at 16:55 UTC | |
by zentara (Archbishop) on Dec 05, 2007 at 14:30 UTC | |
Re: A little demo for Net::SSH2
by perl514 (Pilgrim) on Jan 16, 2013 at 14:42 UTC | |
by Anonymous Monk on Dec 12, 2013 at 18:36 UTC | |
by Anonymous Monk on Mar 14, 2014 at 16:27 UTC | |
Re: A little demo for Net::SSH2
by Anonymous Monk on May 22, 2007 at 12:24 UTC | |
by zentara (Archbishop) on May 22, 2007 at 15:38 UTC | |
by radici (Initiate) on Sep 10, 2008 at 14:23 UTC | |
by TGI (Parson) on Aug 21, 2009 at 03:09 UTC | |
by radici (Initiate) on Sep 10, 2008 at 14:29 UTC | |
by mvrk (Initiate) on Feb 09, 2009 at 19:20 UTC |