Dear monks,

Are there any Net::SSH2 experts in the house?

I'm working with an application at work that pulls code and config out of version control, packages it, pushes it up to a target server, and then deploys the package.

Currently, the communication to the remote servers is done with calls to the ssh binary on the local server. I was hoping to replace these invocations of /usr/bin/ssh with a perl module.

I had a look at Net::SSH:Perl, but had a bit of grief installing that due to the dependencies on various math modules. I'm now working with Net::SSH2, which is certainly a lot easier to install, and I'm not sure whether the problems I'm having are expected behaviour, or a bug.

I would like to be able to issue a single command to a server, reading any output to stdout and stderr, and the exit code of the remote process. I'm trying to use the exec method of Net::SSH2::Channel, but at the moment, I'm having trouble reading the output of my command. If I use the channel object as a file handle and read from that, I typically only get a couple of characters back in output before the SSH channel closes. I've tried using the poll and read methods with slightly more success, but I still only get around a couple of lines from say, 'ls -la' in a directory where I would expect to see ~20 lines of output.

I could use the shell method, but then it seems like there's no easy way to separate stderr for stdout, or to retrieve the exit code.

I wonder if this is expected behaviour for exec. To run a remote command, then immediately exit without waiting for output. Can anyone confirm that?

Here's some sample code and output. Am I doing anything obviously wrong?

#!/usr/bin/perl -w use strict; use Data::Dumper; use Net::SSH2; my $remote_host = 'remote'; my $remote_user = 'vortura'; my $remote_pass = 'xxxxxxx'; my $ssh2 = Net::SSH2->new(); $| = 1; $ssh2->connect($remote_host) or die "connect failed"; $ssh2->auth_password( $remote_user, $remote_pass ) or die "auth failed +"; my $chan = $ssh2->channel(); $chan->blocking(0); my $ret = $chan->exec("ls -la /Users/vortura"); while(<$chan>) { print "OUT: ", $_; } print "\n"; print $ret, "\n"; $chan->close;

Output:

vortura $ ./sshtest.pl OUT: total 0
'total' in the output being the first few lines of output from ls on the remote system.

Many thanks, R


In reply to Printing Output From Net::SSH2 exec by vortura

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.