自动化脚本实践(Shell + Expect)
Linux Shell脚本入门:
Linux awk 命令 | 菜鸟教程
Shell 教程 | 菜鸟教程
linux expect详解
Linux sed 命令 | 菜鸟教程
Linux 批量查找与替换 | 菜鸟教程
参考:华为FusionSphere研发专家YuDeyuan的3ms文章。
实现的功能
- 批量登录目标主机(800+)并执行命令
- 拷贝文件到多台目标主机(800+)
1 准备主机信息
获取需要批量执行的主机IP信息,保存在server.list文件中。注意同一批次的服务器,账号密码要求一致。
# cps host-list | grep 2409:8080 | awk -F' ' '{print $12}'
# vim server.list
IP1
IP2
IP3
2 准备Shell + Expect脚本
The Unix "parameters" :
$#
=> contains the number of parameters for this shell is started
$1
$2
... => parameter 1 parameter 2 ...
$@
=> includes all the parameters that this shell is started
2.1 批量登录目标主机并执行命令
涉及的文件
/home/fsp/login-execute.sh
/home/fsp/login-execute-batch.sh
/home/fsp/server.list
登录单个目标主机并执行命令:login-execute.sh:
#!/usr/bin/expect
# usage: login-execute.sh <remote-ip> <fsp_pwd> <root_pwd> <command>
set timeout 20
set ip [lrange $argv 0 0]
set fsppwd [lrange $argv 1 1]
set rootpwd [lrange $argv 2 2]
set command [lrange $argv 3 end]
spawn ssh -x -o StrictHostKeyChecking=no fsp@$ip
expect "assword:"
send "$fsppwd\r"
expect "fsp@"
send "su root\r"
expect "assword:"
send "$rootpwd\r"
expect "/home/fsp #"
send "$command\r"
expect "/home/fsp #"
send "####################################################\r"
send "exit\r"
expect "fsp@"
send "exit\r"
expect eof
exit
批量登录目标主机并执行命令:login-execute-batch.sh
#!/bin/bash
# usage: login-execute-batch.sh <fsp_pwd> <root_pwd> <command>
if [ $# -eq 0 ]
then
echo usage: login-execute-batch.sh \<fsp_pwd\>\<root_pwd\> \<command\>
exit 1
fi
for i in `cat server.list`
do
echo $i $1
./login-execute.sh $i $@
echo
done
用法
# sh -x login-execute-batch.sh <普通用户fsp密码> <root用户密码> <“在多台服务器同时执行的命令”>
#示例
# sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "sed -i '/10.10.10.11/d' /etc/ntp.conf"
“-x”用来跟踪脚本的执行过程,使shell在执行脚本中把每一个命令行显示出来,并且在行首显示一个"+"号,"+"号后面显示的是经过了变量替换之后的命令行的内容。
2.2拷贝文件到多台目标主机
涉及的文件
/home/fsp/one-host-scp.sh
/home/fsp/batch-host-scp.sh
/home/fsp/server.list
拷贝文件到单个目标主机:one-host-scp.sh:
#!/usr/bin/expect
# usage: one-host-scp.sh <remote-ipv6> <filepathnname> <fsp_pwd>
set timeout 20
set ip [lrange $argv 0 0]
set filepathnname [lrange $argv 1 1]
set fsppwd [lrange $argv 2 end]
spawn scp -o StrictHostKeyChecking=no $filepathnname fsp@\[$ip\]:/home/fsp
expect "s password:"
send "$fsppwd\r"
expect "100%"
send "####################################################\r"
expect eof
exit
拷贝文件到多台目标主机:batch-host-scp.sh:
#!/bin/bash
# usage: batch-host-scp.sh <filepathnname> <fsp_pwd>
if [ $# -eq 0 ]
then
echo usage: batch-host-scp.sh \<filepathnname\> \<fsp_pwd\>
exit 1
fi
for i in `cat server.list`
do
echo $i $1
./one-host-scp.sh $i $@
echo
done
用法
# sh -x batch-host-scp.sh <文件路径与名称> <普通用户fsp密码>
#示例
# sh -x batch-host-scp.sh /home/fsp/file1 Huawei@CLOUD8
“-x”用来跟踪脚本的执行过程,使shell在执行脚本中把每一个命令行显示出来,并且在行首显示一个"+"号,"+"号后面显示的是经过了变量替换之后的命令行的内容。
3 上传脚本与输入数据
上传并修改文件权限
chmod 777 文件名
4 用法
server.list单台测试
拷贝进行测试
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cp /etc/ntp.conf /home/fsp/ntp.conf.test"
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "ll /home/fsp/ntp.conf.test"
查询是否需要修改
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cat /home/fsp/ntp.conf.test | grep 'server 2409:8080:5a0a:500'"
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "ntpq -p"
删除问题行
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "sed -i '/server 2409:8080:5a0a:500/d' /home/fsp/ntp.conf.test"
确认是否删除
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cat /home/fsp/ntp.conf.test | grep 'server 2409:8080:5a0a:500'"
重启查看是否生效
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "service ntpd restart"
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "service ntpd status"
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "ntpq -p"
正式执行
在首节点/home/fsp下执行 (777权限)
查询是否需要修改
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cat /etc/ntp.conf | grep 'server 2409:8080:5a0a:500'"
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "ntpq -p"
先备份
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cp /etc/ntp.conf /etc/ntp.conf.bak"
删除问题行
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "sed -i '/server 2409:8080:5a0a:5004/d' /etc/ntp.conf"
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "sed -i '/server 2409:8080:5a0a:5005/d' /etc/ntp.conf"
确认是否删除
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "cat /etc/ntp.conf | grep 'server 2409:8080:5a0a:500'"
重启查看是否生效
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "service ntpd restart"
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "service ntpd status"
sh -x login-execute-batch.sh Huawei@CLOUD8 Huawei@CLOUD8! "ntpq -p"