Hello again techman2006,
I was thinking about your question and I think I did not answer it correctly before. So I finally got some time today and I came up with this answer which is what I think you expect to see.
I am reading and printing the written file on the terminal, so you can remove this part. I just used it for demonstration purposes.
Working code, and output printed.
#!/usr/bin/perl
use Expect;
use strict;
use warnings;
use Data::Dumper;
use Net::OpenSSH;
use Config::IniFiles;
use Fcntl qw(:flock);
$| = 1;
# To see the complete debuging process
#$Net::OpenSSH::debug = ~0;
my $path = 'conf.ini';
my $timeout = 20;
my $debug = 0;
sub devices {
open my $fh , '<' , "".$path.""
or die "Could not open file: ".$path." - $!\n";
flock($fh, LOCK_SH)
or die "Could not lock '".$fh."' - $!\n";
tie my %ini, 'Config::IniFiles', ( -file => "".$path."" )
or die "Error: IniFiles->new: @Config::IniFiles::errors";
close ($fh)
or die "Could not close '".$fh."' - $!\n";
my @keys = keys (%ini);
my %data = ();
my %final = ();
foreach my $hash (@keys) {
%data = clk_sync( $ini{$hash}{host},
$ini{$hash}{user},
$ini{$hash}{psw},
$ini{$hash}{port},
$hash );
@final{keys %data} = values %data;
}
return %final;
} # end sub complex
my %results = devices();
print Dumper(\%results);
sub clk_sync {
# alternative of shift my ($host,$user,$passwd,$port,$device) = (@_);
my $host = shift;
my $user = shift;
my $passwd = shift;
my $port = shift;
my $device = shift;
my %opts = ( passwd => $passwd,
port => $port,
user => $user );
my $ssh = Net::OpenSSH->new( $host, %opts );
$ssh->error and
die "Couldn't establish SSH connection: ". $ssh->error;
my @commands = ( "ls /tmp" , "vmstat" );
foreach my $command ( @commands ) {
my ($rout, $pid) = $ssh->pipe_out( $command ) or
die "pipe_out method failed: " . $ssh->error;
open my $write, ">>", "sample.txt" or die $!;
print $write "" . $device . " Command executed: " . $command . "\n
+";
while (<$rout>) { print $write $_; }
print $write "\n";
close $rout;
close $write;
}
# Not needed just for demonstration purposes
open my $read, "<", "sample.txt" or die $!;
while (my $row = <$read>) {
chomp $row;
print "$row\n";
}
close $read;
my %hash_out = ( $device => [ @commands ] );
return %hash_out;
}
__END__
DEVICE 1 Command executed: ls /tmp
foo.txt
orbit-tiny
qtsingleapp-Viber-0-3e8
qtsingleapp-Viber-0-3e8-lockfile
ssh-QEhD246wugPL
unity_support_test.0
DEVICE 1 Command executed: vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----
+--cpu-----
r b swpd free buff cache si so bi bo in cs us s
+y id wa st
5 0 152888 584588 22596 1091568 1 8 83 125 771 491 40
+ 6 53 1 0
DEVICE 1 Command executed: ls /tmp
foo.txt
orbit-tiny
qtsingleapp-Viber-0-3e8
qtsingleapp-Viber-0-3e8-lockfile
ssh-QEhD246wugPL
unity_support_test.0
DEVICE 1 Command executed: vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----
+--cpu-----
r b swpd free buff cache si so bi bo in cs us s
+y id wa st
5 0 152888 584588 22596 1091568 1 8 83 125 771 491 40
+ 6 53 1 0
DEVICE 2 Command executed: ls /tmp
foo.txt
orbit-tiny
qtsingleapp-Viber-0-3e8
qtsingleapp-Viber-0-3e8-lockfile
ssh-QEhD246wugPL
unity_support_test.0
DEVICE 2 Command executed: vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----
+--cpu-----
r b swpd free buff cache si so bi bo in cs us s
+y id wa st
1 0 152888 584596 22596 1091568 1 8 83 125 771 492 40
+ 6 53 1 0
DEVICE 1 Command executed: ls /tmp
foo.txt
orbit-tiny
qtsingleapp-Viber-0-3e8
qtsingleapp-Viber-0-3e8-lockfile
ssh-QEhD246wugPL
unity_support_test.0
DEVICE 1 Command executed: vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----
+--cpu-----
r b swpd free buff cache si so bi bo in cs us s
+y id wa st
5 0 152888 584588 22596 1091568 1 8 83 125 771 491 40
+ 6 53 1 0
DEVICE 2 Command executed: ls /tmp
foo.txt
orbit-tiny
qtsingleapp-Viber-0-3e8
qtsingleapp-Viber-0-3e8-lockfile
ssh-QEhD246wugPL
unity_support_test.0
DEVICE 2 Command executed: vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----
+--cpu-----
r b swpd free buff cache si so bi bo in cs us s
+y id wa st
1 0 152888 584596 22596 1091568 1 8 83 125 771 492 40
+ 6 53 1 0
DEVICE 3 Command executed: ls /tmp
foo.txt
orbit-tiny
qtsingleapp-Viber-0-3e8
qtsingleapp-Viber-0-3e8-lockfile
ssh-QEhD246wugPL
unity_support_test.0
DEVICE 3 Command executed: vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----
+--cpu-----
r b swpd free buff cache si so bi bo in cs us s
+y id wa st
1 0 152888 584720 22596 1091568 1 8 83 125 771 492 40
+ 6 53 1 0
$VAR1 = {
'DEVICE 2' => [
'ls /tmp',
'vmstat'
],
'DEVICE 1' => [
'ls /tmp',
'vmstat'
],
'DEVICE 3' => [
'ls /tmp',
'vmstat'
]
};
The conf.ini file is attached in the previous answer.
Hope this is what you where looking for, let me know how it worked for you.
Seeking for Perl wisdom...on the process of learning...not there...yet!
|