如何处理TCL预期输出 [英] How to handle tcl expect output

查看:44
本文介绍了如何处理TCL预期输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个期望脚本以telnet到路由器,进行一些配置,期望一些输出.

I am writing an expect script to telnet to the router ,do some config,expect some output.

如果所需的提示不可用,则它会等待并超时. 因此,我该如何处理并打印错误消息.

If the required prompt is not available then it waits for it and gets time out. So how do i have to handle this and print an error msg.

set timeout 30;

puts "Telnet to $IP 2361\n\n";
spawn telnet $IP 2361;

expect ">";

send "ACT-USER::$user_name:1::$password;";
expect ">";

如果未收到期望值,我该如何处理和打印错误消息?

How do i handle and print an error msg if the expected value is not received?

推荐答案

处理超时需要稍微复杂一些expect:

Dealing with timeouts nicely requires a slightly more complex use of expect:

expect {
    ">" {
        # Got it; don't need to do anything here because we run the code after
    }
    timeout {
        send_user "timed out, oh no!\n"
        exit 1
    }
}

# Now we put the rest of the script...
send "ACT-USER....blah"
# ...

请注意,您的send并没有以\r结尾(以模拟按下 Return 键的结果),我感到很惊讶.

Note that I'm surprised that your send doesn't end in \r (to simulate pressing the Return key).

这篇关于如何处理TCL预期输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆