文章目录
expect
一、expect介绍
expect
是一个免费的编程工具,用来实现自动的交互式任务,而无需人为干预。说白了,expect
就是一套用来实现自动交互功能的软件。需要安装
1、安装xepect
yum install -y expect
2、expect基础
使用
expect
时,基本的核心与四个命令进行交互:
命令 | 作用 |
---|---|
spawn | 启动新的进程 |
expect | 从进程接收字符串 |
send | 用于向进程发送字符串 |
interact | 允许用户交互 |
1)`spawn`命令用来启动新的进程,`spawn`后的`expect`和`send`命令都是和使`spawn`启动的新进程进行交互。
2)`expect`通常用来等待一个进程的反馈,我们根据进程的反馈,再使用`send`命令发送对应的交互命令。
3)`send`命令接收一个字符串参数,并将该参数发送到进程。
4)`interact`命令用的其实不是很多,一般情况下使用`spawn`、`expect`和`send`和命令就可以很好的完成我们的任务;但在一些特殊场合下还是需要使用`interact`命令的,`interact`命令主要用于退出自动化,进入人工交互。比如我们使用`spawn`、`send`和`expect`命令完成了ftp登陆主机,执行下载文件任务,但是我们希望在文件下载结束以后,仍然可以停留在ftp命令行状态,以便手动的执行后续命令,此时使用`interact`命令就可以很好的完成这个任务。
3、expect总结
expect自动应答的基本步骤
第一步: 运行一个程序或命令=> #spawn 命令信息
第二步: 识别产生信息关键字=> #expect 捕获关键字 {send 应答信息}
第三步: 根据识别关键做处理=> #send 应答信息
#脚本中主要使用:
1、spawn 开一个子进程
2、expect 匹配需要交互的内容
3、send 发送交互内容
4、expect eof 结束
4、expect的使用
#在shell脚本中使用
expect <<EOF
expect内容
EOF
#expect匹配问题
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "1\n"}
}
5、expect的执行 (使用环境)
#案列一(在shell解释器中使用)
#!/bin/bash
expect << EOF
spawn ssh root@172.16.1.7 hostname
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "1\n"} #输入回应后,\n进行回车确认
}
expect eof #注意一定要输入exit结束信号
EOF
#案列二(在expect解释器中使用)
#!/usr/bin/expect -f #指定解释器expext
spawn ssh root@172.16.1.7 hostname
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "1\n"}
}
expect eof
[root@web01 hzl]# expect -f hzl1.sh #指定脚本连接测试
spawn ssh root@172.16.1.7 hostname #执行成功
web01
.......
.....
二 、expect实例
1、面交互登录(自动应答)
#!/usr/bin/expect
spawn ssh root@192.168.12.20 uptime #连接并显示系统运行时间及cpu信息
expect "yes/no"
send "yes\n"
expect "*assword"
send "1\n"
expect eof #结束交互
2、名词解释
#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进程,执行交互动作;
3.超时设置
首次登陆之后,再次登陆,就不会出现yes/no的提示了,所以上述脚本再次运行会出现spawn 命令出现交互式提问的expect 匹配不上的情况,此时脚本会阻塞在原地,我们可以set timeout 3设置超时时间,单位为秒,默认情况下是10秒,以保障脚本超时则结束,
#!/usr/bin/expect -f
spawn ssh root@192.168.12.20 uptime
set timeout 3 #某一条expect语句在原地匹配,超过了3秒,无论是否匹配成功都会继续执行下一条指令
expect "yes/no"
send "yes\n"
expect "*assword"
send "1\n"
expect eof
设置超时时间的目的仅仅只是为了让脚本不要一直卡在原地,要真正解决上述问题,需要改写成下述形式
#!/usr/bin/expect -f
spawn ssh root@192.168.12.20 hostname
# 注意:
# 1、{}一定要换行
# 2、下述语句就一个expect,代表匹配了一次,会匹配完一行就匹配下一行
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "1\n"}
}
expect eof
3、案列
[root@aliyun ~]# cat 1.sh
#!/usr/bin/expect -f
spawn ssh egon@127.0.0.1
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 ~]#
4、interact交互
interact
:执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。
[root@egon ~]# cat test.sh
#!/usr/bin/expect -f
spawn ssh root@192.168.12.20
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "1\n"}
}
interact
[root@egon ~]#
[root@egon ~]# ./test.sh
spawn ssh root@192.168.12.20
root@192.168.12.20'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 ~]#
三、为expect脚本传参
shell脚本中的变量无法直接在expect中使用的,若expect需要使用变量
1)一方面可以自己定义
#!/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
2)另外一方面可以通过下述方式引入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中,
$argc
表示参数个数,而参数值存放在$argv
中,比如取第一个参数就是[lindex $argv 0]
,以此类推。
[root@egon ~]# cat test.sh
#!/usr/bin/expect -f
if {$argc != 4} {
puts "Usage:./script.sh <ip> <username> <password> <cmd>"
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 root@192.168.12.20 hostname
root@192.168.12.20's password:
egon
[root@egon ~]#
能够在工作中熟练的使用Shell脚本就可以很大程度的提高工作效率,如果再搭配上expect,那么很多工作都可以自动化进行,对工作的展开如虎添翼。如果你会Python的话,你的视野将会更加开阔,那个时候你又会“嫌弃”expect了