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.).


In reply to Re^2: Problem in connecting to remote unix host via Net::SSH2 from windows machine by salva
in thread Problem in connecting to remote unix host via Net::SSH2 from windows machine by Perl_abcd_monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.