script说明
我们单位的物业经常发布断电维护通知,每次断电都需要将所有服务器关掉。这个脚本可以让你做这件事变的简单
- #!/usr/bin/perl -w
- use strict;
- use Net::SSH::Expect;
- open (AF, "host_list") or die "don't open host_list: $!\n";
- my @list;
- foreach (<AF>) {
- chomp;
- @list=split(/\s/);
- SHUTDOWN("$list[0]","$list[1]","$list[2]","$list[3]");
- };
- close AF;
- sub SHUTDOWN {
- my $host=shift;
- my $user=shift;
- my $password=shift;
- my $wait=shift;
- my $ssh=Net::SSH::Expect->new (
- host=>$host,
- password=>$password,
- user=>$user,
- raw_pty=>1,
- );
- $ssh->debug(0);
- $ssh->run_ssh() or die "SSH process couldn't start: $!";
- sleep 3;
- if ($ssh->waitfor('\(yes\/no\)\?', 2)) {
- $ssh->send("yes\n");
- };
- $ssh->waitfor('password', 5);
- $ssh->send("$password");
- $ssh->waitfor("$wait", 2);
- #$ssh->exec("stty raw -echo");
- my $cmd=$ssh->exec("init 0");
- # print "$cmd\n\n";
- print "$host: --------------------------------------------------\n";
- $ssh->close();
- }
host_list.txt 格式如下: ip地址 用户名 密码 主机名 ----------------------------- 10.13.55.11 root 1234 gaia 10.13.55.10 root aaa- rdfs 10.13.55.30 root bbb- dns 10.13.55.12 root 1234 dns 10.13.55.17 root 1234 dotproject 10.13.55.19 root cccc ftp