in reply to Re: redirect stdout into hash
in thread redirect stdout into hash

Thanks, but the array doesn't seem to be populating when used in the subroutine?

Replies are listed 'Best First'.
Re^3: redirect stdout into hash
by aaron_baugher (Curate) on Jun 13, 2012 at 19:46 UTC

    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 ----------------------------------------------

        You're reading from the channel before you execute the command through it. Look at my example again.

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