tcl - 有没有办法在 TCL 中使用 IF CONDITION 期望语句?
问题描述
有没有办法我可以 IF CONDITION 一个期望命令。例如在下面的代码中,我尝试 ssh 到特定的 IP 地址,有时它会问我“你确定要继续连接(是/否)吗?” 有时它不会。我如何做到这一点,如果它显示我发送“是/r”,如果它没有,我继续我的代码?
set name="123.456.78.910"
expect "$ " ; send -- "ssh root@$name\r"
expect {
"Password:" { send "password" }
timeout { sleep 1 }
}
expect "Are you sure you want to continue connecting (yes/no)?" ; send -- "yes\r”
解决方案
这是exp_continue
命令的目的:
spawn ssh user@host
set prompt {\$ $} ;# or whatever regex your prompt matches
expect {
timeout {error "timed out connecting to host"}
"continue connecting" {send "yes\r"; exp_continue}
"Password:" {send "$password\r"; exp_continue}
-re $prompt
}
send "do stuff\r"
# ...
如果期望超时,脚本会出错。
如果您收到“继续连接”提示,请发送“是”并继续期待。
如果您收到“密码”提示,请发送您的密码并继续期待。
当你得到你的 shell 提示时,expect
命令完成并且你继续你的程序。
推荐阅读
- c - 我正在尝试使用指针合并两个排序的(int)数组,并且由于某种原因它存储了地址
- android - 完成 Firebase 函数(Uri 和字符串)Android Java 时出现 ClassCastException
- vim - Vim-airline 在底部产生间隙
- geometry - 检查矩形是否在三角形内的快速方法(2D)
- laravel - 在本地存储图像 vs cloudinary vs s3
- python - Python 3.6:导入子目录在 Pycharm 中不起作用
- javascript - 从内部箭头函数访问方法变量
- java - HttpUrl queryParameters 转换为 MultiValuedMap
- python - 将字典转换为数据框的一列,同时将字典行名保留在另一列中(python)
- java - 如何在向其中插入数据期间阻止检索 ArrayList 信息