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?

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

In reply to How to capture process and redirect STDOUT in a hash 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.