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.


In reply to Writing to an SSH channel by BernieC

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.