Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Problem in connecting to remote unix host via Net::SSH2 from windows machine

by salva (Canon)
on Jun 12, 2017 at 08:25 UTC ( [id://1192565]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1192565]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found