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.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Net::OpenSSH::Parallel with sudo commands by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.