Hi, channel usage can be a bit tricky. See Net::SSH2 Interactive command example and Net::SSH2 just hangs up without any output The first thing to try the non-blocking mode on the channel.
$chan->blocking(1);
UPDATE:, as per salva's suggestion to use latest code. With the latest Net::SSH2 the following works for me on linux. Any use of $channel->blocking(0) immediately gives an error message, so $chan->blocking(1) is almost mandatory.
#!/usr/bin/perl use warnings; use strict; use Net::SSH2; # assuming a user named 'z' for demonstration # connecting to localhost, so you need your sshd running my $ssh2 = Net::SSH2->new(); $ssh2->connect('localhost') or die "Unable to connect Host $@ \n"; $ssh2->auth_password('z','ztester') or die "Unable to login $@ \n"; my $chan = $ssh2->channel(); $chan->blocking(1); $chan->exec('ls -la'); while (<$chan>){ print } # run a second command in $chan $chan->exec('dir'); while (<$chan>){ print } #will get dir named / thru another channel my $chan1 = $ssh2->channel(); $chan1->blocking(1); $chan1->exec('ls -la /'); while (<$chan1>){ print } #shell use my $chan2 = $ssh2->channel(); $chan2->blocking(1); $chan2->shell(); #opens shell on server print $chan2 "uname -a\n"; print "LINE1 : $_" while <$chan2>; # from perldoc Net::SSH2::Channel # Note that only one invocation of "process" or any of the shortcuts # "shell", "exec" or "subsystem" is allowed per channel. In order t +o run # several commands, shells or/and subsystems, a new "Channel" insta +nce # must be used for every one. # only 1 command per shell, the next line won't run print $chan2 "who\n"; print "LINE2 : $_" while <$chan2>; $chan2->close; $ssh2->disconnect(); exit; __END__

I'm not really a human, but I play one on earth. ..... an animated JAPH

In reply to Re: Net:SSH2 channels by zentara
in thread Net:SSH2 channels 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.