通过expect脚本远程批量管理服务器
自从工作性质从以前的集成项目支撑转为现在的服务器运维之后,手里所负责的服务器数量也变的相当多了。
所以免不了做许多重复的琐碎工作,特别是针对同一个集群下的相同类型的服务器,操作步骤等等都完全相同,虽然已经通过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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#!/bin/bash for i in `cat config/hosts.txt` do export server=`echo $i | awk -F "|" '{print $1}'` export port=`echo $i | awk -F "|" '{print $2}'` export user=`echo $i | awk -F "|" '{print $3}'` export passwd=`echo $i | awk -F "|" '{print $4}'` export rootpasswd=`echo $i | awk -F "|" '{print $5}'` export cmdfile="config/commands.txt" ./expect-run.exp $server $port $user $passwd $rootpasswd $cmdfile done |
expect-run.exp
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#!/usr/bin/expect -f # heylinux.com # Check if { $argc<6 } { send_user "usage: $argv0 <server> <port> <user> <passwd> <rootpasswd> <cmdfile> \n" exit } # TIMEOUT set timeout 20 # Login parameters set server [lindex $argv 0] set port [lindex $argv 1] set user [lindex $argv 2] set passwd [lindex $argv 3] set rootpasswd [lindex $argv 4] set cmdfile [ open [lindex $argv 5] ] # Logfile log_file log/run.log # Login Server spawn ssh -p $port $user@$server ## Enable this and Disable the "spawn ssh ..." above if you are using ssh-key. #spawn ssh -i ssh-key/Identity.ppk -p $port $user@$server expect { "yes/no)?\ " {send "yes\r";exp_continue} "*assword:\ " {send "$passwd\r"} ## Disable the "*assword:\ ..." above if you are using ssh-key, and Enable this if your ssh-key has passphrase. # "Identity.ppk':\ " {send "$passwd\r"} } # Login as Root expect "*]$\ " {send "su - root\r"} expect "*assword:\ " {send "$rootpasswd\r"} # Run Commands expect "*]#\ " { while {[gets $cmdfile cmd] >= 0} { send "$cmd\r" } } # Exit Root expect "*]#\ " {send "exit\r"} # Exit User expect "*]$\ " {send "exit\r"} # Close File close $cmdfile # Exit Expect expect eof |
hosts.txt
1 2 3 |
192.168.10.200|22|username|userpasswd|rootpasswd 444.333.222.111|22|username|userpasswd|rootpasswd login.server.com|7033|username|userpasswd|rootpasswd |
commands.txt
1 2 3 4 |
cd /opt ls -l sleep 5 tail -n 20 /var/log/message |
最后,注意如果是通过ssh密钥来验证的话,需要修改expect-run.exp脚本中的Login部分(第27,30,35,38行);
而如果在脚本中还需要执行更多的交互式内容,如批量更新用户密码等,建议直接修改expect-run.exp脚本,在脚本中直接进行,总之这个脚本给我们的管理提供了一个模板,它并不适用于所有情况,但略加修改后是可以的。