实现同时管理多台服务器的expect脚本,多台expect下面是利用expec
实现同时管理多台服务器的expect脚本,多台expect下面是利用expec
最近通过exploringexpect书籍,简单学了下expect脚本语言,这个脚本语言是tcl语言的扩展,用来解决一些工具无法自动交互的问题,如ssh登录时,无法在命令就指定密码等。下面是利用expect来实现管理多台服务器的简单例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/expect
#purpose:auto run command on multiple servers
#how to: mms <user> <cmd>
#write by zhumaohai.
#blog:http://www.linuxboy.net/
if{$argc < 2} {
puts"usage: mms <user> <cmd>"
exit1
}
#set servers
setSERVERS {"192.168.0.100""192.168.0.101""192.168.0.102"}
#set password
setPASSWORDS(user1)"passwd1"
setPASSWORDS(user2)"passwd2"
#get virables
setUSER [lindex $argv 0]
setCMD [lrange $argv 1 end]
setpasswd$PASSWORDS($USER)
foreach x $SERVERS {
evalspawnssh-l $USER $x $CMD
expect {
"password"{ send"$passwdr"}
"yes/no"{ send"yesr";exp_continue; }
}
expect eof
}
1、这里定义了三台服务器192.168.0.100 192.168.0.101 192.168.0.102,定义了用户user1的密码为passwd1,用户user2的密码为passwd2,假如脚本文件名为ms,用法为:
./ms 用户 命令
如./ms user1 date
2、在使用脚本时,请确认系统已经安装有expect命令,centos使用yum install expect安装,ubuntu使用apt-get install expect安装。
评论暂时关闭