Does the code you provided work for you on linux ? It doesn't for me. Seems to me that as soon as you '$chan->shell();' you lose the capability to get the output. There's simply nothing there to be read. To get the output, I find I have to '$chan->exec("command");'. Here's a demo that works for me on Windows:
use Net::SSH2;
use warnings;
use strict;
my $buflen = 100;
my $buf = '0' x $buflen;
my $ssh2 = Net::SSH2->new();
$ssh2->connect('192.168.0.3') or die;
$ssh2->auth_password('username','password')
or die "Unable to login $! \n";
my $chan1 = $ssh2->channel();
$chan1->blocking(1);
$chan1->exec('echo $PATH');
$chan1->read($buf, $buflen);
print "BUF: ", $buf, "\n";
$chan1->close;
my $chan2 = $ssh2->channel();
$chan2->blocking(1);
$chan2->exec('echo $HOME');
$chan2->read($buf, $buflen);
print "BUF: ", $buf, "\n";
$chan2->close;
It outputs:
BUF: /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
BUF: /home/rob
If someone can provide me with a *nix script that both uses the shell() method and captures the output, then I'll take a look at replicating that on Windows.
Cheers,
Rob
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.