snmp配置文件同步脚本

   由于使用CACTI监控软件需要将受控服务器snmpd.conf文件添加授权主机,如果在大量集群坏境下,这样一个一个主机的配置效率会很低,所以我近期编写了一个多线程的同步配置文件脚本来实现此功能。

 

#!/usr/bin/env perl

 

use warnings;

use strict;

 

use Net::OpenSSH;

use threads;

 

my @th_list;

my $user = 'root';

my %param = (

user => $user,

timeout => 30

);

my %hosts = (

‘你的主机名’=> '主机IP',

);

 

#

#MAIN

#

 

my @hostgrp = &ipLoop;

foreach (@hostgrp) {

push @th_list, threads->create(\&mutiSCP, $_);

}

 

foreach (0..$#hostgrp){

  $th_list[$_]->join();

}

 

print "\tDone!\n";

 

#

#FUNCTION

#

 

sub ipLoop {

  my @all_ip;

  foreach (values(%hosts)) {

    my @split_ip = split / /,$_;

    foreach (@split_ip) {

      push @all_ip,$_;

    }

  }

return @all_ip;

}

 

sub mutiSCP{

my $host = shift;

my $ssh = Net::OpenSSH->new($host, %param);

$ssh->scp_put("/etc/default/snmpd", "/etc/default/") or die "can't connect $!".$ssh->error;

$ssh->scp_put("/etc/snmp/snmpd.conf", "/etc/snmp/") or die "can't connect $!".$ssh->error;

my ($stdout, $stderr) = $ssh->capture2("/etc/init.d/snmpd restart");

if (!$stdout and $stderr) {

print $ssh->error;

print $stderr;

}else{

print "<$_>".$stdout;

}

}

 

你可能感兴趣的:(职场,休闲,snmpd)