http://www.perlmonks.org?node_id=1192565


in reply to Re: Problem in connecting to remote unix host via Net::SSH2 from windows machine
in thread Problem in connecting to remote unix host via Net::SSH2 from windows machine

Actually, your original sub works fine for me so long as I precede the call to $chan2->shell(); with $chan2->blocking(0);

Using non-blocking mode in that way is quite unreliable!

The remote shell started by the shell method call runs a loop where it waits for new commands to be submitted and that's the reason why print "LINE : $_" while <$chan2> blocks; the remote shell is just waiting for a new command to arrive in order to execute it. It nevers sends the EOF which would terminate the while.

Setting non-blocking mode causes <$chan2> to read data immediately available. So you depend on the remote command and the SSH talk on the network bringing its output faster than the perl script reading it.

The right way to handle that is to look for the shell prompt to arrive (you have to ensure it can not appear in the command output), or to make the remote command generate its output in some way such that its end can be detected (i.e. appending some marker, preppending the length, using chunk-encoding, etc.).

Replies are listed 'Best First'.
Re^3: Problem in connecting to remote unix host via Net::SSH2 from windows machine
by syphilis (Archbishop) on Jun 12, 2017 at 11:56 UTC
    Using non-blocking mode in that way is quite unreliable!

    Thanks for pointing that out.
    The shell() method is something that I prefer to avoid and, in light of your explanation, I think I'll continue trying to avoid it.

    Cheers,
    Rob