thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to figure out how to execute multiple sudo cmds with parallel ssh. I have manage successfully to create multiple sequential ssh requests and capture the output.
I tried to follow the instructions at Net::OpenSSH::Parallel @ FAQ - Frequently Asked Questions. I am doing something wrong but I can not figure out where I am going so wrong.
Important Notice In order to make the script to work you need to save all the ssh keys to the host that the script will be executed. "~/.ssh/known_hosts"
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
Update: Removing unnecessary code adding minimum needed code:
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 $timeout = 20; 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); foreach my $hash ( @mps ) { $pssh->add_host( $ini{$hash}{host} , user => $ini{$hash}{user}, port => $ini{$hash}{port}, passwd => $ini{$hash}{psw} ); $sudo_passwords{$ini{$hash}{host}} = $ini{$hash}{psw}; } my @cmd = ( "sudo service ntp stop" , "sudo ntpd -gq" , "sudo service ntp start" ); sub sudo { my ($label, $ssh, @cmd) = @_; $ssh->system({stdin_data => "$sudo_passwords{$label}\n"}, 'sudo', '-Skp', '', '--', @cmd); } $pssh->push('*', parsub => \&sudo, @cmd); #$pssh->push( '*' , command => 'ls' , '/home/username/Downloads' ) +; $pssh->run; return %ini; } # end sub complex my %results = devices();
Output Update: I am getting this error when executing the code above.
Enter Password: Enter Password: sudoerssudoers: sudo service ntp stop: + command not found Request rejected by Privilege Manager : sudo service ntp stop: command not found Request rejected by Privilege Manager
Update: 2 Script works but how to capture output?
Thanks to i5513 I manage to make it work correctly, but I am having difficulties on capturing and redirecting the desired output. I am trying to capture the time slew and store it with the host as hash ref.
Sample of solution code:
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 ( $_ !~ /ntpd$/ ); } }
Has anybody similar problem and possible solution?
Thank you in advance for your time and effort.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Net::OpenSSH::Parallel with sudo commands
by Anonymous Monk on Dec 30, 2014 at 00:25 UTC | |
|
Re: Net::OpenSSH::Parallel with sudo commands
by soonix (Chancellor) on Dec 30, 2014 at 12:44 UTC | |
by thanos1983 (Parson) on Dec 30, 2014 at 14:17 UTC | |
|
Re: Net::OpenSSH::Parallel with sudo commands
by i5513 (Pilgrim) on Dec 30, 2014 at 13:04 UTC | |
by thanos1983 (Parson) on Dec 30, 2014 at 14:24 UTC |