简体   繁体   English

使用Expect的密码提示不起作用

[英]Password prompt using Expect not working

I need to accept a password from keyboard containing at least one number, at least one uppercase letter, at least one lowercase letter and optional symbols # & - I wrote the following code. 我需要从键盘接受一个密码,该密码包含至少一个数字,至少一个大写字母,至少一个小写字母和可选符号#&-我编写了以下代码。 It always times out, but not detecting the password... Can anyone help 它总是超时,但是没有检测到密码...谁能帮忙

package require Expect
log_user 0
send "Enter Password\n"
expect  {
-re {(?=.*?[0-9])(?=.*?[a-z])(?=.*?[A-Z])[a-zA-Z0-9#&-]*} {send "Password ok"}
    timeout { send "Time out" }
    }

I had edited the code and now it is working only if the password is given in correct format at the first try...If a wrong format is given in the first try, expect will wait for the next try. 我已经编辑了代码,现在只有在第一次尝试以正确的格式输入密码时才可以使用...如果第一次尝试输入的格式不正确,则期望将等待下一次尝试。 But even if the next entry is correct, Expect is not detecting it and is going for the next try and finally time outs 但是,即使下一个输入正确,Expect也不会检测到它,而是要进行下一次尝试,最终超时

You need another rule to match an unacceptable password (ie, anything ending in a newline that doesn't match the other rule), print a message, and start waiting again. 您需要另一个规则来匹配不可接受的密码(即,以换行符结尾的任何内容都与另一个规则不匹配),打印一条消息,然后再次开始等待。 The key to this is exp_continue . 关键是exp_continue

send "Enter Password\n"
expect  {
    -re {(?=.*?[0-9])(?=.*?[a-z])(?=.*?[A-Z])([a-zA-Z0-9#&-]*)[\r\n]+} {
        send "Password ok"
        # Save the password for later...
        set thePassword $expect_out(1,string)
    }
    -re {[\r\n]+} {
        send "That looks like a bad password. Try again please.\n"
        exp_continue
    }
    timeout {
        send "Time out"
    }
}

The exp_continue command says “keep on expecting; exp_continue命令说“保持期望;保持期望”。 we're not done yet” and it is vital for dealing with complex interactions. 我们还没有完成”,这对于处理复杂的交互至关重要 Also, I've anchored the match at the end of the line and added () to make the password easier to extract. 另外,我将匹配项固定在行的末尾,并添加了()以使密码更易于提取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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