# expect 用法
- spawn 开启一个新进程
- expect 读取终端返回内容
- send 发送字符串到终端
- interact 保持在远程终端,如不使用,将直接断开连接
# 示例
# expect交互的脚本
#!/usr/bin/expect
# 连接跳板机
spawn ssh -pPORT USERNAME@HOST
# 如果返回的内容包含*yes/no,发送yes并且换行
expect {
"*yes/no" {send "yes\r"; exp_continue}
"*password:" {send "password\r"}
}
# 保持在远端
interact
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 执行方法
在执行中发现使用 ./xxx.sh 或 bash xxx.sh 均不好使,最后发现执行需要用expect进行执行
expect -f xxx.sh
1