简体   繁体   中英

How to handle tcl expect output

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM