I tried it with chan->blocking(0) disabled ...
I think that chan->blocking(0) is the correct thing to do. It works fine that way for me. The while{} block at the end of your code puzzles me (and emits a couple of warnings). Is there a problem with coding it simply as:
use strict;
use Net::SSH2;
my $ssh2 = Net::SSH2->new();
$ssh2->connect('host')
or die "Unable to connect host $@ \n";
$ssh2->auth_password('user','pass');
my $buflen = 512;
my $buf2 = '0' x $buflen;
my $chan = $ssh2->channel();
$chan->blocking(0);
$chan->shell();
$chan->write("ls\n");
select(undef,undef,undef,0.25);
my $len = $chan->read($buf2,$buflen);
print $buf2, "\n";
Update: Another option is to use exec() instead of shell():
my $chan = $ssh2->channel();
$chan->exec('ls');
$chan->read($buf2, $buflen);
print $buf2, "\n";
With this option, blocking is not disabled, and no select call needs to be made.
(Btw, I didn't know that the shell method could be made to work with a select() call .... so I've learnt something today.)
Cheers, Rob |