How to handle tcl expect output

2019-08-01 07:46发布

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?

标签: tcl expect
1条回答
2楼-- · 2019-08-01 08:07

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).

查看更多
登录 后发表回答