How to handle tcl expect output

*爱你&永不变心* 提交于 2019-12-24 13:39:27

问题


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?


回答1:


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"
# ...

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



来源:https://stackoverflow.com/questions/12315151/how-to-handle-tcl-expect-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!