I don't think the get_channel sub is reliable. If for whatever reason the data coming from the remote side is delayed, the non-blocking read call is going to return undef and any additional data will be lost.

Doing non-blocking IO with Net::SSH2 is far from being easy. You can see how it is done in my module Net::SSH::Any using select to check when new data arrives on the SSH socket (the code is here, see the __io3 method).

Another possibility that usually works is to send all the input data (login and commands) in one go and then capture everything in blocking mode until you get and EOF. You will have to add an additional command to close the connection from the remote side (i.e. exit).