Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
UPDATE 28 Sept 2018 This is an added comment on the blocking() issue which seems to cause many problems for many people, and is relatively poorly understood by myself, for sure. In the script below, I suggest commenting out all blocking() lines, if you run into problems with channel usage. If need be, take a practical approach, and use trial and error in your situation on your platform... try blocking(1), try blocking(0), and if all fails, just try commenting out blocking(). From the perldoc:
####################################################
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__

In reply to A little demo for Net::SSH2 by zentara

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-03-28 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found