in reply to Writing to an SSH channel

You may like to check Net::SSH::Any. It works on top of Net::SSH2 (and other SSH modules), and provides a much higher level and easy to use API.

It also takes into account a lot of Net::SSH2 quirks and corner cases you are not handling.

my $ssh = Net::SSH::Any->new(..., backend => 'Net_SSH2'); my ($out, $err) = $ssh->capture2({stdin_data => $sendtext}, $cmd);