Hi,

This works for me on Win32, using Net::SSH2-0.18, built against an older version of libssh2, on perl-5.8:
use Net::SSH2; use warnings; use strict; my $buflen = 10000; my $buf1 = '0' x $buflen; my $ssh2 = Net::SSH2->new(); $ssh2->connect('xxx.xxx.xxx.xxx') or die; $ssh2->auth_password('xxx','xxxxxxx') or die "Unable to login $! \n"; my $chan = $ssh2->channel(); $chan->blocking(0); $chan->shell(); for(1 .. 2) { if($_ == 1) {$chan->write("echo \$PATH\n")} else {$chan->write("echo \$HOME\n")} select(undef,undef,undef,0.25); $chan->read($buf1, $buflen); print "BUF1: ", $buf1, "\n"; } $chan->close;
However, running Net::SSH2-0.28, built against the latest libssh2-1.2.2, the buffer is *empty* in the first iteration - but filled correctly in subsequent iterations of the loop.

That is, with the older version of Net::SSH2 I'm getting:
BUF1: /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/games:/home/rob +/bin BUF1: /home/rob
but with the latest, I'm getting:
BUF1: BUF1: /home/rob
I can hack my way around the problem with the latest Net::SSH2 by changing the loop to:
for($i = 0, $i < 2, $i++) { if($_ == 1) {$chan->write("echo \$PATH\n")} else {$chan->write("echo \$HOME\n")} select(undef,undef,undef,0.25); $chan->read($buf1, $buflen); if($buf1) {print "BUF1: ", $buf1, "\n"} else { print "Empty buffer ... trying again\n"; --$i; } }
That then produces (running the latest Net::SSH2):
Empty buffer ... trying again BUF1: /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/games:/home/rob +/bin BUF1: /home/rob
Hope there's something there that helps. (I don't have time to "tune" this script at the moment - or to look at the issue with the latest version.)

Cheers,
Rob

In reply to Re: Net::SSH2 to execute for loop by syphilis
in thread Net::SSH2 to execute for loop by godsown

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.