Why do you need a hash? An array might be simpler. With a hash, you will need to increment a hash key counter, whearas with an array, you just keep pushing each recv into the array in order received. Anyways, for a hash, something like this would work. There is one glitch, since you are reading a $buf of size 1024, you have no idea where the newlines are going to be, so each hash entry may be an incomplete line, which is continued in the next hash entry. It may be better to concantate all received $buf into one long string, then split the string on newlines at put those lines into a hash.
use warnings;
use Net::SSH2;
use Data::Dumper;
my $ssh = Net::SSH2->new();
# make a global hash for storage
my %hash;
# make a global counter for incrementing the hash keys
my $counter = 0;
......
......
if($ssh->auth_ok())
{
runcmd(my_channel($ssh), "$bigcmd");
}
# read output ( but lines may be split )
print $DATA::Dumper( \%hash ),"\n";
__END__
sub runcmd
{
my ($channel, $command) = @_;
$channel->exec($command);# or die $@ . $ssh->error();
my $buff;
while(!$channel->eof())
{
my $buff;
$channel->read($buff, 1024);
# print $buff;
$hash{$counter} = $buf;
$counter++;
}
my $rc = $channel->exit_status();
$channel->close();
return $rc;
}
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.