expect是一个免费的编程工具,用来实现自动的交互式任务,而无需人为干预。说白了expect就是一套用来实现自动交互功能的软件需要自行安装
yum install -y expect
expect基础
在使用expect时,基本上都是和以下四个命令打交道:
命令 | 作用 |
---|---|
spawn | 启动新的进程 |
expect | 从进程接收字符串 |
send | 用于向进程发送字符串 |
interact | 允许用户交互 |
总结expect自动应答的基本步骤
第一步: 运行一个程序或命令=> spawn 命令信息
第二步: 识别产生信息关键字=> expect 捕获关键字 {send 应答信息}
第三步: 根据识别关键做处理=> send 应答信息
自动应答脚本
#!/usr/bin/expect
spawn ssh [email protected] uptime
#捕捉是否信任设备
expect "yes/no"
send "yes\n"
#捕捉密码
expect "*assword"
send "1\n"
expect eof
解释
#1、#!/usr/bin/expect -f:使用expect来解释该脚本
#2、spwan:
spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。它主要的功能是给ssh运行进程加个壳,用来传递交互指令;
#3、expect:
expect "*assword":这里的expect也是expect的一个内部命令,这个命令的意思是判断上次输出结果里是否包含“password”的字符串,如果有则立即返回;否则就等待一段时间后返回,这里等待时长就是前面设置的30秒;
#4、send:
send "1\n":当匹配到对应的输出结果时,就发送密码到打开的ssh进程,执行交互动作;
首次登陆之后,再次登陆,就不会出现yes/no的提示了,所以上述脚本再次运行会出现spawn 命令出现交互式提问的expect 匹配不上的情况,此时脚本会阻塞在原地,我们可以set timeout 3设置超时时间,单位为秒,默认情况下是10秒,以保障脚本超时则结束,
#!/usr/bin/expect -f
spawn ssh [email protected] uptime
set timeout 3 # 某一条expect语句在原地匹配,超过了3秒,无论是否匹配成功都会继续执行下一条指令
expect "yes/no"
send "yes\n"
expect "*assword"
send "1\n"
expect eof
设置超时时间的目的仅仅只是为了让脚本不要一直卡在原地,要真正解决上述问题,需要改写成下述形式
#!/usr/bin/expect -f
read -p "请输入你想连接的IP地址:" ip
expect -c "
spawn ssh $ip
expect {
# \"yes/no\" {send \"yes\r\";exp_continue}
# \"*asswd\" {send \"1\n\"}
"yes/no" {send "yes\r";exp_continue}
"*asswd" {send "1\n"}
}"
# 注意
# 1、{}一定要换行
# 2、上述语句就一个expect,代表匹配了一次,会匹配完一行就匹配下一行
练习
[root@aliyun ~]# cat 1.sh
#!/usr/bin/expect -f
spawn ssh [email protected]
set timeout -1 # 设置为-1代表永不超时,如果expect没有捕捉到就一直停在原地
expect {
"yes/no" {send "yes\n"}
}
expect {
"password" {send "1\n"}
}
expect "*egon*"
send "ls\n"
expect "\$"
send "pwd\n"
expect "\$"
send "exit\n" # 注意一定要输入exit结束信号
expect eof # 最后关闭匹配
[root@aliyun ~]#
interact交互
interact:执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。
[root@egon ~]# cat test.sh
#!/usr/bin/expect -f
spawn ssh [email protected]
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "1\n"}
}
interact
[root@egon ~]#
[root@egon ~]# ./test.sh
spawn ssh [email protected]
[email protected]'s password:
Last login: Wed Aug 26 21:28:04 2020 from egon
+--------------------------------------------+
| |
| 你当前登录的是支付业务后台数据库服务 |
| 请不要删库 |
| |
+--------------------------------------------+
[root@egon ~]# pwd
/root
[root@egon ~]# echo "hello"
hello
[root@egon ~]# exit
登出
Connection to 192.168.12.20 closed.
[root@egon ~]#
shell脚本中的变量无法直接在expect中使用的,若expect需要使用变量
一方面可以自己定义
#!/usr/bin/expect -f
set timeout -1
set user "root"
set ip "192.168.12.20"
set cmd "hostname"
set pass "1"
spawn ssh $user@$ip $cmd
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "$pass\n"}
}
expect eof
另外一方面可以通过下述方式引入shell变量,注意此时解释器换成#!/bin/bash
#!/bin/bash
user="root"
ip="192.168.12.20"
cmd="hostname"
pass="1"
expect << EOF
spawn ssh $user@$ip $cmd
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "$pass\n"}
}
expect eof
EOF
此外,expect脚本还可以从命令行获取参数
在expect中, a r g c 表 示 参 数 个 数 , 而 参 数 值 存 放 在 argc表示参数个数,而参数值存放在 argc表示参数个数,而参数值存放在argv中,比如取第一个参数就是[lindex $argv 0],以此类推。
[root@egon ~]# cat test.sh
#!/usr/bin/expect -f
if {$argc != 4} {
puts "Usage:./script.sh "
exit 1
}
set ip [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
set cmd [lindex $argv 3]
set timeout -1
spawn ssh $user@$ip $cmd
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "$pass\n"}
}
expect eof
[root@egon ~]# ./test.sh
Usage:./script.sh <ip> <username> <password> <cmd>
[root@egon ~]# ./test.sh 192.168.12.20 root 1 hostname
spawn ssh [email protected] hostname
[email protected]'s password:
egon
[root@egon ~]#
免密脚本
[root@m01 ~]# cat ssh.sh
ssh-keygen -R 172.16.1.5
ssh-keygen -R 172.16.1.6
ssh-keygen -R 172.16.1.7
ssh-keygen -R 172.16.1.8
ssh-keygen -R 172.16.1.9
ssh-keygen -R 172.16.1.31
ssh-keygen -R 172.16.1.41
ssh-keygen -R 172.16.1.51
ssh-keygen -R 172.16.1.71
yum install expect -y
for ip in 'lb01' 'lb02' 'web01' 'web02' 'web03' 'backup' 'nfs' 'db' 'm01' 'prometheus'
do
expect -c "
spawn ssh-copy-id -i root@$ip
expect {
\"(yes/no)\" {send \"yes\r\";exp_continue}
\"password\" {send \"1\r\";exp_continue}
} "
done
能够在工作中熟练的使用Shell脚本就可以很大程度的提高工作效率,如果再搭配上expect,那么很多工作都可以自动化进行,对工作的展开如虎添翼。如果你会Python的话,你的视野将会更加开阔,那个时候你又会“嫌弃”expect了。