Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...If there has ever been a justification needed for access to the net during working hours, just referring to this site should suffice. Fantastic!..."

Geography

Where in the world do Tek-Tips members come from?

Redirecting STDOUT back into hash

czarj (TechnicalUser)
13 Jun 12 9:20
Hi Folks,
How can I redirect stdout back into a hash in my script (susphash)? Here I am logging into a remote server and running a command. I want all the lines of the command put into a hash. Is there a way to redirect that output that is more elegant than writing to a temp_file and then reading it into my hash?

CODE

#!/usr/software/bin/perl -w
use strict;
use warnings;
use Net::SSH2;

my $ssh = Net::SSH2->new();
$ssh->debug(0);
my $bigcmd = "version";
my %susphash = ();

$ssh->connect("11.61.92.10",22);
my $pass = 'P@ssw0rd';
$ssh->auth(username => 'root', password =>$pass );

if($ssh->auth_ok())
{
runcmd(my_channel($ssh), "$bigcmd");
}

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

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.

czarj (TechnicalUser)
13 Jun 12 16:26
I decided to use an array instead of a hash. This works.

CODE

#!/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";
$/ = '';
print @array, "\n";


sub runcmd
{
my ($channel, $command) = @_;
$channel->exec($command);# or die $@ . $ssh->error();
while(!$channel->eof())
{
my $buff;
$channel->read($buff, 1024);
push @array, $buff;
}
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;
}

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close