Maybe a little less code would have made it easier also :-)
#!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__
HTHpackage Scanner; use strict; use warnings; use Config::IniFiles; our %fh_log=(); our %fh_err=(); sub new { my ($class,$conf,$dir) = @_; tie my %conf, 'Config::IniFiles', ( -file => $conf ) or die "Error: IniFiles->new: @Config::IniFiles::errors"; tie my %dir, 'Config::IniFiles', ( -file => $dir ) or die "Error: IniFiles->new: @Config::IniFiles::errors"; my %obj = ( 'conf' => \%conf, 'dir' => \%dir, 'log' => {}, ); return bless \%obj, $class; } sub makeDir { my $self = shift; my $hr = $self->{dir}->{Directories}; foreach my $dir ( keys %$hr) { my $path = $hr->{$dir}; unless (-e $path) { mkdir ($path , 0755); print "Created path $path\n"; } else { print "Exists - $path\n"; } } } sub get_labels { my $self = shift; return keys %{$self->{'conf'}}; } sub get_opts { my ($self,$mp)=@_; my $conf = $self->{'conf'}{$mp}; my $dir = $self->{'dir'}{'Directories'}; my $log = join '/', $dir->{'log_dir'},$conf->{'log'};; open $fh_log{$mp},'>',$log or die "Could not open $log : $!"; my $err = join '/', $dir->{'err_dir'},$conf->{'error'};; open $fh_err{$mp},'>',$err or die "Could not open $err : $!"; my $opts = { user => $conf->{'user'}, port => $conf->{'port'}, password => $conf->{'psw'}, default_stderr_fh => $fh_err{$mp}, default_stdout_fh => $fh_log{$mp}, }; return ($conf->{'host'},$opts); } sub parse_log { my ($self,$mp) = @_; my $conf = $self->{'conf'}{$mp}; my $dir = $self->{'dir'}{'Directories'}; my $log = join '/', $dir->{'log_dir'},$conf->{'log'};; open LOG,'<',$log or die "Could not open $log : $!"; my $last; while (<LOG>){ chomp; next unless /\S/; if (/(Ubuntu|Crux|openSUSE)/i){ $self->{'log'}{$mp}{'OS'} = uc $1; } $last = $_; } $self->{'log'}{$mp}{'hostname'} = $last; close LOG; } # ntpd sub get_ntp_cmd { my ($self,$mp) = @_; my $OS = $self->{'log'}{$mp}{'OS'}; my $psw = $self->{'conf'}{$mp}{'psw'}; my @cmd = ('echo %HOST%','','ntpd -gq',''); if ($OS eq 'UBUNTU'){ $cmd[1] = 'service ntp stop'; $cmd[3] = 'service ntp start'; } if ($OS eq 'CRUX'){ $cmd[1] = '/etc/rc.d/ntpd stop'; $cmd[3] = '/etc/rc.d/ntpd start'; } #if ($OS eq 'OPENSUSE'){ # @cmd = ('cat /etc/ntp.conf'); #} return ($psw,@cmd); } sub close_logs { for (keys %fh_log){ print "Closing $_\n"; close $fh_log{$_} or die "could not close $fh_log{$_} : $!"; close $fh_err{$_} or die "could not close $fh_err{$_} : $!"; } } 1;
In reply to Re^5: Use of uninitialized value $selector in split at /usr/local/share/perl/5.18.2/Net/OpenSSH/Parallel.pm line 141.
by poj
in thread Use of uninitialized value $selector in split at /usr/local/share/perl/5.18.2/Net/OpenSSH/Parallel.pm line 141.
by thanos1983
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |