#!perl use strict; use warnings; use Config::IniFiles; use Net::OpenSSH::Parallel; use Data::Dump 'pp'; use Scanner; # config my $CONF_FILE = "conf.ini"; my $DIR_FILE = "directories.ini"; # read ini files and prepare folders my $scan = Scanner->new($CONF_FILE,$DIR_FILE); $scan->makeDir(); # get list of hosts to process my @labels = $scan->get_labels(); my $max_workers = @labels; my %opts = ( workers => $max_workers, connections => $max_workers * 2, reconnections => 3, ); # set up ssh sessions my $pssh = Net::OpenSSH::Parallel->new(%opts); print "Adding Hosts to Net::OpenSSH::Parallel\n"; for my $label (@labels){ my ($host,$opts) = $scan->get_opts($label); $pssh->add_host($label,$host,%$opts); print " $host added as label '$label'\n" } # run cat commands my $cmd = 'cat /etc/issue; echo %HOST%'; print "Running $cmd\n"; $pssh->push('*', command => $cmd ); $pssh->run; print "Completed\n\n"; # parse log to get OS and host # and run ntp commands print "Running ntp commands\n"; for my $label (@labels){ $scan->parse_log($label); my ($psw,@cmd) = $scan->get_ntp_cmd($label); $pssh->push($label , parsub => \&sudo_cmd , $psw, @cmd); } $pssh->run; # tidy up $scan->close_logs(); print "Completed\n\n"; pp $scan; # sudo command sub sudo_cmd { my ($label , $ssh , $psw, @cmd) = @_; foreach my $c (@cmd) { $ssh->system( {stdin_data => "$psw\n"} , 'sudo' , '-Skp' , '' , '--' , split " " , $c ); } } __END__