in reply to redirect stdout into hash

Since the Net::SSH::Channel object is also a tied filehandle, you can avoid all the C-style read buffering:

$channel->exec($command); my @array = <$channel>; # read output lines into an array # or my %hash = map { $_ => undef } <$channel>; # read output lines into a +hash's keys

Aaron B.
Available for small or large Perl jobs; see my home node.

Replies are listed 'Best First'.
Re^2: redirect stdout into hash
by doctorspears (Novice) on Jun 13, 2012 at 19:22 UTC
    Thanks, but the array doesn't seem to be populating when used in the subroutine?

      Show your code. If you instantiate the array with my within the subroutine, it won't be available outside the subroutine, unless you return it.

      Aaron B.
      Available for small or large Perl jobs; see my home node.

        #!/usr/software/bin/perl -w use strict; use warnings; use Net::SSH2; my $ssh = Net::SSH2->new(); $ssh->debug(0); my $bigcmd = "priv set diag; wafl_susp -w"; my @array; $ssh->connect("10.61.92.10",22); my $pass = 'P@ssw0rd'; $ssh->auth(username => 'root', password =>$pass ); if($ssh->auth_ok()) { runcmd(my_channel($ssh), "$bigcmd"); } print "----------------------------------------------\n"; foreach (@array) { print "$_\n"; } sub runcmd { my ($channel, $command) = @_; @array = <$channel>; $channel->exec($command);# or die $@ . $ssh->error(); my $buff; while(!$channel->eof()) { my $buff; $channel->read($buff, 1024); } my $rc = $channel->exit_status(); $channel->close(); return $rc; } sub my_channel { my $ssh = shift(@_); my $chan = $ssh->channel('session'); $chan->blocking ( 0 ); $chan->flush(); $chan->ext_data('merge'); return $chan; }

        results in

        @cycsnane01:( ~/_Scripts/) $ ./waflsusp.pl ----------------------------------------------