vishapat has asked for the wisdom of the Perl Monks concerning the following question:

Hi I'm having issues with active perl and the ssh2 module. Here's a snip of my code ---------------------------------------------------- my $buflen = 512; my $buf2 = '0' x $buflen; my $chan = $ssh2->channel(); $chan->shell(); $chan->write("ls\n"); select(undef,undef,undef,0.25); $len = $chan->read($buf2,$buflen); while ($len > 0) { print $buf2; $buf2 = '0' x $buflen; print "\nGoing into the read\n"; $len = $chan->read($buf2,$buflen); }" ---------------------------------------------------- Here's the debug when it starts hanging ---------------------------------------------------- Net::SSH2::Channel::read(size = 512, ext = 0) - read 294 bytes ---------------------------------------------------- It seems blocking until there is the exact amount. I tried it with chan->blocking(0) disabled and I don't get the full output. Also, is it possible once I have this ssh connection established to make another ssh connection through this connection?

Replies are listed 'Best First'.
Re: Active Perl & SSH2
by syphilis (Archbishop) on Feb 25, 2009 at 00:26 UTC
    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
Re: Active Perl & SSH2
by imrags (Monk) on Feb 25, 2009 at 06:39 UTC