自从工作性质从以前的集成项目支撑转为现在的服务器运维之后,手里所负责的服务器数量也变的相当多了。
所以免不了做许多重复的琐碎工作,特别是针对同一个集群下的相同类型的服务器,操作步骤等等都完全相同,虽然已经通过rsync将shell脚本放到每台服务器上再执行的方式来简化工作,但还是需要重复的进行登陆,况且很多时候只是想执行一条简单的口令,并查看结果。
因此,我了解了一些关于批量管理服务器方面的知识,在这方面比较知名的有Puppet和func,其中Puppet在全世界很多著名互联网公司都得到了应用。但要学习并部署好这样的系统还是需要一些时间的,而且还涉及到对每台服务器的更改,主要是软件的安装配置。
为了能够快速解决眼前的问题,我查询了一些expect脚本相关的资料,它可以用来处理交互式的命令,因此可以用来实现自动登录和执行命令,并将执行结果打印到log文件中。下面是我在生产环境中得到成功应用的一个expect脚本,为了让它更具有复用性,我将需要管理的主机和命令都写到了配置文件中,通过脚本读取的方式来执行。
整个脚本的构成如下:
其中,
config目录下存放的是commands.txt批处理命令与hosts.txt服务器配置列表;
log目录下存放的是运行的日志信息;
ssh-key目录下存放的是ssh私钥文件,权限必须为600;
expect-run.exp是expect脚本文件,需要可执行权限;
main-shell.sh是主执行程序,需要可执行权限,通过./main-shell.sh执行,用于从hosts文件循环取值并调用expect脚本。
整个脚本内容可通过这里下载:http://heylinux.com/download/shell-expect-remote.tgz
下面,我将配置文件与脚本的相关内容展示给大家:
main-shell.sh
01 |
#!/bin/bash |
02 |
|
03 |
for i in ` cat config/hosts.txt` |
04 |
do |
05 |
export server=` echo $i | awk -F "|" '{print $1}' ` |
06 |
export port=` echo $i | awk -F "|" '{print $2}' ` |
07 |
export user=` echo $i | awk -F "|" '{print $3}' ` |
08 |
export passwd =` echo $i | awk -F "|" '{print $4}' ` |
09 |
export rootpasswd=` echo $i | awk -F "|" '{print $5}' ` |
10 |
|
11 |
export cmdfile= "config/commands.txt" |
12 |
|
13 |
./expect-run.exp $server $port $user $ passwd $rootpasswd $cmdfile |
14 |
done |
expect-run.exp
01 |
#!/usr/bin/expect -f |
02 |
# heylinux.com |
03 |
|
04 |
# Check |
05 |
if { $argc<6 } { |
06 |
send_user "usage: $argv0 <server> <port> <user> <passwd> <rootpasswd> <cmdfile> \n" |
07 |
exit |
08 |
} |
09 |
|
10 |
# TIMEOUT |
11 |
set timeout 20 |
12 |
|
13 |
# Login parameters |
14 |
set server [lindex $argv 0] |
15 |
set port [lindex $argv 1] |
16 |
set user [lindex $argv 2] |
17 |
set passwd [lindex $argv 3] |
18 |
set rootpasswd [lindex $argv 4] |
19 |
set cmdfile [ open [lindex $argv 5] ] |
20 |
|
21 |
# Logfile |
22 |
log_file log/run.log |
23 |
|
24 |
# Login Server |
25 |
spawn ssh -p $port $user@$server |
26 |
|
27 |
## Enable this and Disable the "spawn ssh ..." above if you are using ssh-key. |
28 |
#spawn ssh -i ssh-key/Identity.ppk -p $port $user@$server |
29 |
|
30 |
expect { |
31 |
"yes/no)?\ " {send "yes\r" ;exp_continue} |
32 |
|
33 |
"*assword:\ " {send "$passwd\r" } |
34 |
|
35 |
## Disable the "*assword:\ ..." above if you are using ssh-key, and Enable this if your ssh-key has passphrase. |
36 |
# "Identity.ppk':\ " {send "$passwd\r"} |
37 |
} |
38 |
|
39 |
# Login as Root |
40 |
expect "*]$\ " {send "su - root\r" } |
41 |
expect "*assword:\ " {send "$rootpasswd\r" } |
42 |
|
43 |
# Run Commands |
44 |
expect "*]#\ " { |
45 |
while {[gets $cmdfile cmd] >= 0} { |
46 |
send "$cmd\r" |
47 |
} |
48 |
} |
49 |
|
50 |
# Exit Root |
51 |
expect "*]#\ " {send "exit\r" } |
52 |
|
53 |
# Exit User |
54 |
expect "*]$\ " {send "exit\r" } |
55 |
|
56 |
# Close File |
57 |
close $cmdfile |
58 |
|
59 |
# Exit Expect |
60 |
expect eof |
hosts.txt
1 |
192.168.10.200|22|username|userpasswd|rootpasswd |
2 |
444.333.222.111|22|username|userpasswd|rootpasswd |
3 |
login.server.com|7033|username|userpasswd|rootpasswd |
commands.txt
1 |
cd /opt |
2 |
ls -l |
3 |
sleep 5 |
4 |
tail -n 20 /var/log/message |
最后,注意如果是通过ssh密钥来验证的话,需要修改expect-run.exp脚本中的Login部分(第27,30,35,38行);
而如果在脚本中还需要执行更多的交互式内容,如批量更新用户密码等,建议直接修改expect-run.exp脚本,在脚本中直接进行,总之这个脚本给我们的管理提供了一个模板,它并不适用于所有情况,但略加修改后是可以的。
注:
set timeout 30
设置超时时间的,计时单位:秒
spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。所以不要用 “which spawn“之类的命令去找spawn命令。好比windows里的dir就是一个内部命令,这个命令由shell自带,你无法找到一个dir.com 或 dir.exe 的可执行文件。它主要的功能是给ssh运行进程加个壳,用来传递交互指令。
expect “password:”
这里的expect也是expect的一个内部命令,expect的shell命令和内部命令是一样的,www.linuxidc.com但不是一个功能。这个命令的意思是判断上次输出结果里是否包含“password:”的字符串,如果有则立即返回,否则就等待一段时间后返回,这里等待时长就是前面设置的30秒
send “nopasswd\r”
这里就是执行交互动作,与手工输入密码的动作等效。
温馨提示: 命令字符串结尾别忘记加上 “\r”,如果出现异常等待的状态可以核查一下。
interact
执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。如果你只是登录过去执行一段命令就退出,可改为〔expect eof〕