expect 实现自动化命令下发
背景
随着业务增长需要管理的主机数量也随之增多,在很多情况下需要对批量的主机进行配置变更,然而一台台的修改主机的配置显然是不现实的,也不符合当下运维趋势。能用机器实现的就不要以人肉的方式做无用功,向自动化运维方向迈进。接下来介绍如何使用 expect 实现对批量 Linux 主机和网络设备的命令下发配置。
expect 版本
rpm -qa expect
expect 路径
which expect
expect 安装
yum -y install expect
参数介绍
set timeout : 等待多少秒退出脚本,-1(永不超时)
spawn : spawn 后为具体要执行的命令
expect : 定义字符内容用于匹配上面spawn后面执行命令的返回内容
send : 如果spawn后面运行命令的返回内容,匹配expect上面定义的。就发送send定义的内容到上面。
exp_continue : 处于expect代码段内,表示重新回到expect开始出执行命令。
interact: 允许用户交互
expect eof: 交互完关闭expect
举例1
实现 linux 主机登录执行命令的基础交互
cat expect.sh
#!/usr/bin/expect
set user root
set password elk-node2
set ip 192.168.99.186
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
expect "]#" { send "who am i\n" }
expect "]#" { send "exit\n" }
expect eof
赋予可执行权限
chmod a+x expect.sh
图片
举例2
使用for 循环调用文本文件实现交互下发命令
cat expect01.sh
#!/usr/bin/bash
password=elk-node2
for ip in `cat /mnt/shell/ip.txt01`
do
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
/usr/bin/expect <<-EOF
set timeout 10
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
expect "]#" { send "df -hT\n" }
expect "]#" { send "exit\n" } expect eof
EOF
fi
done
文本文件内容
cat ip.txt01
192.168.99.186
赋予可执行权限
chmod a+x expect01.sh
图片
举例3
使用while 循环实现批量主机命令下发
cat expect02.sh
#!/usr/bin/bash
cat /mnt/shell/ip.txt | while read ip password
do
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
/usr/bin/expect <<-EOF
set timeout 10
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
expect "]#" { send "free -h\n" }
expect "]#" { send "sed -i 's/UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config\n" }
expect "]#" { send "systemctl restart crond\n" }
expect "]#" { send "exit\n" }
expect eof
EOF
else
echo -e "\e[1;35m$ip host ping down...\e[0m"
fi
done
文本文件内容
cat ip.txt
192.168.99.32 2222
192.168.99.185 elk-node1
192.168.99.186 elk-node2
192.168.99.244 1234
赋予可执行权限
chmod a+x expect02.sh
图片
举例四
scp 拷贝文件到批量主机
cat expect03.sh
#!/usr/bin/bash
cat /mnt/shell/ip.txt | while read ip password
do
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
/usr/bin/expect <<-EOF
set timeout 10
spawn scp /mnt/shell/hs1.sh root@$ip:/opt
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
expect eof
EOF
else
echo -e "\e[1;35m$ip host ping down...\e[0m"
fi
done
文本文件内容
cat ip.txt
192.168.99.32 2222
192.168.99.185 elk-node1
192.168.99.186 elk-node2
192.168.99.244 1234
赋予可执行权限
chmod a+x expect02.sh
图片
举例五
网络设备批量执行命令(华三设备为例)
cat expect05.sh
#!/usr/bin/bash
cat /mnt/shell/ip.txt | while read ip username password
do
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
/usr/bin/expect <<-EOF
set timeout 10
spawn telnet $ip
expect "Username:" { send "$username\n" }
expect "Password:" { send "$password\n" }
expect ">" { send "display ip interface brief\n" }
expect ">" { send "display cpu-usage\n" }
expect ">" { send "quit\n" }
expect eof
EOF
else
echo -e "\e[1;35m$ip host ping down...\e[0m"
fi
done
文本文件内容
cat ip.txt
172.18.1.30 username password
172.18.1.14 username password
赋予可执行权限
chmod a+x expect05.sh
图片
网友评论