【问题标题】:Is there a way to IF CONDITION a expect statement in TCL? [duplicate]有没有办法在 TCL 中使用 IF CONDITION 期望语句? [复制]
【发布时间】:2020-06-05 06:10:17
【问题描述】:

有没有办法我可以 IF CONDITION 一个期望命令。例如在下面的代码中,我试图 ssh 到一个特定的 IP 地址,有时它会问我“你确定要继续连接(是/否)吗?”有时它不会。如何使 IF 它显示我发送“yes/r”而 IF 它没有,我继续我的代码?

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”

【问题讨论】:

  • 我并不是要结束这个问题,抱歉。无论如何,你有一个答案。

标签: tcl expect


【解决方案1】:

这就是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 命令完成,您继续你的程序。

【讨论】:

    猜你喜欢
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2023-03-25
    相关资源
    最近更新 更多