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?
--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
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.