I am very close to finish a TCL/TK app that logs into a Cisco Access Point via a serial connection (RS232} and gives it an IP address (very basic)

However, I would like my script to attempt a secondary password if the first one fails

This is how the Cisco CLI behaves with a serial connection when the incorrect password is entered 3 times (No User Name is needed, only prompts for a password)

Password:
Password:
Password:
% Bad secrets

Again, if the default password of "Cisco" does not work, I need the script to try the secondary password of "Cisco2"

The following is my most recent unsuccessful attempt at this problem.

    expect "*>" {send "en\r"}

    expect {
    "Password:" {send "Cisco\r"; exp_continue}
    "Password:" {send "Cisco2\r"; exp_continue}
    }

    expect "*#" {send "config t\r"}

Thanks in advance for the help.

有帮助吗?

解决方案

The simplest way is to have a list of passwords to try which you step through:

set passwords {"Cisco" "Cisco2"}
set idx 0
expect "*>"
send "en\r"
expect {
   "Password:" {
      send "[lindex $passwords $idx]\r"
      incr idx
      exp_continue;   # Continue to wait for the "after" prompt
   }
   "*#" {send "config t\r"}
}

The trick is that you have to also expect the thing that comes after, so that you don't fall back on timeouts or things like that. (Well, assuming you don't want timeouts. If you do, go right ahead!) This is because expect waits for all its match clauses simultaneously. In your buggy code, you had two clauses with the same match text, so it was always picking the first of them (IIRC, if multiple clauses match at the current point, the first possible branch is the one chosen).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top