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:
'total' in the output being the first few lines of output from ls on the remote system.vortura $ ./sshtest.pl OUT: total 0
Many thanks, R
In reply to Printing Output From Net::SSH2 exec by vortura
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |