thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
This question is a sequence of Net::OpenSSH::Parallel with sudo commands. Although that the question has been answered I am facing a new problem that I can not overcome.
The script is operating perfectly, it adds the registered hosts and applies the set of commands. The problem is that I can not figure out how to capture and process the STDOUT.
I have found a way to capture the output and paste it into a file. I am trying to process the output before pasting it on the file. I want to process the output into a hash according to the device that is executing the code.
Sample of current output:
* Stopping NTP server ntpd ...done. * Stopping NTP server ntpd ...done. ntpd: time slew +0.000740s ntpd: time slew +0.013857s * Starting NTP server ntpd * Starting NTP server ntpd ...done. ...done.
The desired output would be something like:
IP1 => {ntpd: time slew +0.000740s} IP2 => {ntpd: time slew +0.013857s}
Sample of conf.ini file.
[MP 101] host = 127.0.0.1 user = username psw = password port = 22 [MP 100] host = 127.0.0.1 user = username psw = password port = 22
Sample of code with successful parallel ssh connections with sudo multiple commands.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Fcntl qw(:flock); use Config::IniFiles; use Net::OpenSSH::Parallel; select STDOUT; $| = 1; select STDERR; $| = 1; my %dev_data = (); my %sudo_passwords = (); sub devices { my $path = 'conf.ini'; 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 @mps = keys ( %ini ); my $maximum_workers = @mps; my $maximum_connections = 2 * $maximum_workers; my $maximum_reconnections = 3; my %opts = ( workers => $maximum_workers, connections => $maximum_connections, reconnections => $maximum_reconnections ); my $pssh = Net::OpenSSH::Parallel->new(%opts); #open my $stdout_fh, '>>', 'test.log' or die $!; foreach my $hash ( @mps ) { $pssh->add_host( $ini{$hash}{host} , user => $ini{$hash}{user}, port => $ini{$hash}{port}, password => $ini{$hash}{psw} ); #default_stdout_fh => $stdout_fh ); $sudo_passwords{$ini{$hash}{host}} = $ini{$hash}{psw}; } my @cmd = ( "service ntp stop" , "ntpd -gq" , "service ntp start" ); sub sudo { my ($label, $ssh, @cmd) = @_; foreach my $c (@cmd) { $ssh->system( {stdin_data => "$sudo_passwords{$label}\n"} , 'sudo' , '-Skp' , '' , '--' , split " " , $c ); ( $dev_data{$label} = $_ ) if ( $_ !~ /slew/ ); } } $pssh->push('*', parsub => \&sudo, @cmd); $pssh->run; #print Dumper(\%dev_data); return %dev_data; } # end sub complex my %results = devices();
Does anybody know how to do that?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to capture process and redirect STDOUT in a hash
by graff (Chancellor) on Jan 02, 2015 at 04:49 UTC | |
by thanos1983 (Parson) on Jan 02, 2015 at 08:41 UTC | |
by graff (Chancellor) on Jan 02, 2015 at 18:17 UTC | |
|
Re: How to capture process and redirect STDOUT in a hash
by salva (Canon) on Jan 02, 2015 at 11:28 UTC | |
|
Re: How to capture process and redirect STDOUT in a hash
by locked_user sundialsvc4 (Abbot) on Jan 02, 2015 at 16:16 UTC |