|Welcome to the Monastery|
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