I'm not sure where the right place here on perlmonks is to share coding examples. But.. after much flailing about i've managed to tame ssh2 {and auth and all of that} and I have a working sub that does exactly what I want and I thought I'd share. My approach to using the SSH2 connection is to send commands, poring over the output of each to decide what to do next until I"m done.
# NB: you can only do one command on a channel so we get the channel ## do the command give back the output then close the channel Note +that the command ## must be a fully "shell punctuated and escaped" sub docmd { my $chan = $ssh2->channel() or $ssh2->die_with_error ; $chan->exec("($_[0]) 2>&1") ; # my $out = ""; while (!$chan->eof) { my $buffer = ""; my $bytes = $chan->read($buffer, 100) ; $ssh2->die_with_error if $bytes < 0 ; $out .= $buffer ; } return $out ; }
and I tested it with:
print docmd("cd tmp; ls") ; print docmd("cd bin; ls") ; $ssh2->disconnect() ;

and I confess that I was amazed that it worked.


In reply to Running commands with Net::SSH2 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.