TCL / Expect Scripting - Using a conditional statement to attempt a secondary login password

陌路散爱 提交于 2019-12-24 08:55:46

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/11332920/tcl-expect-scripting-using-a-conditional-statement-to-attempt-a-secondary-lo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!