Hi, channel usage can be a bit tricky. See
Net::SSH2 Interactive command example and
Net::SSH2 just hangs up without any output The first thing to try the non-blocking mode on the channel.
$chan->blocking(1);
UPDATE:, as per salva's suggestion to use latest code. With the latest Net::SSH2 the following works for me on linux. Any use of $channel->blocking(0) immediately gives an error message, so $chan->blocking(1) is almost mandatory.
#!/usr/bin/perl
use warnings;
use strict;
use Net::SSH2;
# assuming a user named 'z' for demonstration
# connecting to localhost, so you need your sshd running
my $ssh2 = Net::SSH2->new();
$ssh2->connect('localhost') or die "Unable to connect Host $@ \n";
$ssh2->auth_password('z','ztester') or die "Unable to login $@ \n";
my $chan = $ssh2->channel();
$chan->blocking(1);
$chan->exec('ls -la');
while (<$chan>){ print }
# run a second command in $chan
$chan->exec('dir');
while (<$chan>){ print }
#will get dir named / thru another channel
my $chan1 = $ssh2->channel();
$chan1->blocking(1);
$chan1->exec('ls -la /');
while (<$chan1>){ print }
#shell use
my $chan2 = $ssh2->channel();
$chan2->blocking(1);
$chan2->shell(); #opens shell on server
print $chan2 "uname -a\n";
print "LINE1 : $_" while <$chan2>;
# from perldoc Net::SSH2::Channel
# Note that only one invocation of "process" or any of the shortcuts
# "shell", "exec" or "subsystem" is allowed per channel. In order t
+o run
# several commands, shells or/and subsystems, a new "Channel" insta
+nce
# must be used for every one.
# only 1 command per shell, the next line won't run
print $chan2 "who\n";
print "LINE2 : $_" while <$chan2>;
$chan2->close;
$ssh2->disconnect();
exit;
__END__