BernieC has asked for the wisdom of the Perl Monks concerning the following question:

I'm baffled by Net::SSH2::Channel. I have a script, working quite well :o), that runs a command on a channel via "exec" and collects the output. What's baffling me is how to send data *to* the command. There's stuff I don't fully understand about blocking mode and strange errors and such. I understand about the difficulties of running interactive commands remotely, but that's not what I want to do. I just want to feed a bunch of data to the command and then wait for the answer. I tried modifying my {sub docode} routine {which is probably still over in the Cool Uses section} to take text-to-send as an argument:
{ my $sendtext = $_[1] ; # new my $chan = $ssh2->channel() or $ssh2->die_with_error ; $chan->blocking(1) ; #new $chan->exec("($_[0]) 2>&1") ; my $write = $chan->write($sendtext) ; #new $ssh2->die_with_error() unless $write ; #new if ($write != length($sendtext)) #new { die "Only $write bytes were sent!\n" ; } #new my $out = ""; while (!$chan->eof) { my $buffer = ""; if (not defined ($chan->read($buffer, 100))) { $ssh2->die_with_error() ; } $out .= $buffer ; }

I checked by calling it with docmd("cat >sshfile", "this is a test")and I was delighted to see

~$ cat sshfile This is a test

So I *can* write to the ssh connection but the 'read' never returns.. it just hangs-- I just don't know how to get read and write to play nice together.

Replies are listed 'Best First'.
Re: Writing to an SSH channel
by salva (Canon) on Sep 19, 2018 at 07:26 UTC
    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);
Re: Writing to an SSH channel
by Marshall (Canon) on Sep 18, 2018 at 20:23 UTC
    I haven't used this exact module. I am curious about this:  $chan->blocking(1) ; You want a non-blocking read that returns immediately if no data is available to be read. Maybe this flag is wrong? Update: I'll need to study this a bit more - it been a while since I've done such things. You may also need some kind of a flush to get the write to happen. I think this is more involved than my first impression.
      I managed to fix the problem. As I pondered it over dinner, I thought about the mysterious send_eof() and it did the trick: now the program works just fine. A $chan->send_eof() and it all seems to work..
        Sounds like "You may also need some kind of a flush to get the write to happen." Worked by sending eof! FANTASTIC!