近期要安装N多台Linux主机的NRPE客户端,话说我这么“懒”的人怎么可能会一个个自己安.批量处理的简单小程序就用perl来做吧!
# tar -tf BatchNrpe.tar.gz BatchNrpe/ BatchNrpe/install.sh BatchNrpe/Check_Nrpe.pl BatchNrpe/nrpe-2.15.tar.gz BatchNrpe/Batch_Installation_Nrpe.pl BatchNrpe/hostlist BatchNrpe/nagios-plugins-1.5.tar.gz |
BatchNrpe.tar.gz为主主程序包.
install.sh是自动部署NRPE的Shell脚本
useradd nagios /bin/tar -zxvf nagios-plugins-1.5.tar.gz |
hostlist是需要安装NRPE的主机列表
#IC Server #格式 IP->密码 |
nagios-plugins-1.5.tar.gz nrpe-2.15.tar.gz是安装必须要的Tarball
Batch_Installation_Nrpe.pl用于自动scp必要的Tarball和install.sh安装脚本至hostlist列表主机
#!/bin/env perl # # This program is used to batch install NRPE # # # Created : Yumeng # Creation Date : 4 December 2013 # # E-mail : mixmaomao@163.com # # Use Statement # use strict; use warnings; use Expect; # # Read the installation list # File Format: IP address->Passwd # such as : 172.20.1.1->password # open HOSTLIST,"<","hostlist" or die "Can't open file :$!\n"; my @hostlist = <HOSTLIST>; close HOSTLIST; # my %hostlist; for (@hostlist) { chomp; next if m{^#}; my $host; my $pass; $host = (split(/\->/))[0]; $pass = (split(/\->/))[1]; $hostlist{$host}=$pass; } # # Login host # while (my ($host,$pass)=each %hostlist) { my $exp_scp = Expect->new; $exp_scp = Expect->spawn("scp install.sh nagios-plugins-1.5.tar.gz nrpe-2.15.tar.gz $host:/root"); $exp_scp->expect(2, [ 'password', sub { my $self_scp = shift; $self_scp->send("$pass\n"); } ], [ '\(yes/no\)?', sub { my $self_scp = shift; $self_scp->send("yes\n"); exp_continue; } ], ); #$exp->send("exit\n") if ($exp->expect(undef,'#')); my $exp_ssh = Expect->spawn("ssh $host"); $exp_ssh->expect(2, [ 'password', sub { my $self = shift; $self->send("$pass\n"); } ], [ '\(yes/no\)?', sub { my $self = shift; $self->send("yes\n"); exp_continue; } ], ); $exp_ssh->send("sh install.sh\n") if ($exp_ssh->expect(undef,'#')); $exp_ssh->send("exit\n") if ($exp_ssh->expect(undef,'#')); } |
Check_Nrpe.pl用于检测NRPE是否连通
#!/bin/env perl open HOSTLIST,"<","hostlist" or die "Can't open file :$!\n"; |