目前我们已经使用expect编程编写了一个登录方法。在那里,它需要密码,如果超时,它就会出错。我们在tcl中为此编写了以下代码:
expect {
-i $var -re ".*(yes/no)." {
send -i $var "yesr"
expect {
-i $var -re ".*pass" {
send -i $var "$pwdr"
}
timeout {
puts "Check IP and Password ...timed out"
return 0
}
}
}
-i $var -re ".*pass" {
send -i $var "$pwdr"
expect {
-i $var -re ".*Permission denied" {
exp_continue
}
-i $var -re "Permission denied" {
puts "login not succesful - Check IP and Password"
return 0
}
}
}
timeout {
puts "login not succesful, Check IP and Password ... timed out"
return 0
}
puts "Connection established."
现在我们观察到,即使登录成功,代码也在等待超时时间结束,因此它正在消耗一些时间。
所以谁能建议如何返回成功一旦登录发生,而不是等待超时到期?
使用exp_continue
,我们可以简单地处理这个问题。
set prompt "#|>|\$"; # Some commonly used prompts
# We escaped dollar symbol with backslashes, to treat it as literal dollar
expect {
-i $var
timeout {puts "Timeout happened"; return 0}
"(yes/no)" {send -i $var "yesr";exp_continue}
-re ".*pass" {send -i $var "$pwdr";exp_continue}
"Permission denied" {puts "Permission denied";return 0;}
-re $prompt {puts "Login successful!!!";return 1}
}