【问题标题】:How to implement "fallback condition" in while loop in Expect?如何在 Expect 的 while 循环中实现“回退条件”?
【发布时间】:2016-08-05 05:56:48
【问题描述】:

我有一台机器可以通过 telnet 登录,然后通过“ctrl+C”直到看到提示。 ctrl+C 可能并不总是有效,因此我必须每 5 秒尝试一次,直到看到预期的输出 ($prompt)。

如果我没有收到 $prompt,我如何确保我可以有效地重试 while 循环?此代码是否低于最佳实践?我担心的是,我不知道当“ctrl+C”失败时我会得到什么,它可能是任何东西,除非它是 $prompt,否则它必须被忽略。

while { $disableFlag == 0 } {
    send "^C\r"
    expect {
                "*$prompt*" {
                    puts "Found the prompt"
                    sleep 5
                }
                "*" {
                    set disableFlag 1
                    puts "Retrying"
                    sleep 5
                }
    }
}

【问题讨论】:

    标签: while-loop expect


    【解决方案1】:

    你可能想要这样的东西(未经测试)

    set timeout 5
    send \03
    expect {
        "*$prompt*" {
            puts "found the prompt"
        }
        timeout {
            puts "did not see prompt within $timeout seconds. Retrying"
            send \03
            exp_continue
        }
    }
    
    # do something after seeing the prompt
    

    \03 是 ctrl-C 的八进制值:参见 http://wiki.tcl.tk/3038

    如果你想最终退出:

    set timeout 5
    set count 0
    send \03
    expect {
        "*$prompt*" {
            puts "found the prompt"
        }
        timeout {
            if {[incr count] == 10} {  # die
                error "did not see prompt after [expr {$timeout * $count}] seconds. Aborting"
            }
            puts "did not see prompt within $timeout seconds. Retrying"
            send \03
            exp_continue
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-25
      • 2014-02-15
      • 1970-01-01
      • 2022-09-23
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 2014-06-26
      相关资源
      最近更新 更多