1

我正在编写一个期望脚本来远程登录到路由器,做一些配置,期望一些输出。

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

set timeout 30;

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

expect ">";

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

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

4

1 回答 1

3

很好地处理超时需要稍微复杂一点的使用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按键)结尾。

于 2012-09-07T10:57:23.337 回答