Tcl Expect脚本在“新建密码”提示下无误结束

Tcl Expect脚本在“新建密码”提示下无误结束,tcl,expect,Tcl,Expect,我有Solaris服务器,我不确定是否已将其密码更改为特定帐户。我将成功地进行身份验证,因为我已经更改了密码,或者我将使用旧密码进行身份验证,它将在我使用旧密码进行身份验证后提示我更改密码,因为旧密码已过期 Warning: Your password has expired, please change it now. New Password: 脚本将在新密码:提示下停止,并在没有错误的情况下退出 #!/bin/bash NewPassword="cccccc" OtherPasswo

我有Solaris服务器,我不确定是否已将其密码更改为特定帐户。我将成功地进行身份验证,因为我已经更改了密码,或者我将使用旧密码进行身份验证,它将在我使用旧密码进行身份验证后提示我更改密码,因为旧密码已过期

Warning: Your password has expired, please change it now.
New Password: 
脚本将在
新密码:
提示下停止,并在没有错误的情况下退出

#!/bin/bash

NewPassword="cccccc"
OtherPassword="ffffffff"

for i in `cat x.txt`

do

/opt/csw/bin/expect -f  <(cat << EOF

spawn ssh -o StrictHostKeyChecking=no adminuser@$i
expect "Password: "
send "$NewPassword\r"

expect {
"$ " { 
## new password worked 
     send "uname -a\r"
}
"Password: " {
 ## The new password did not work 
    send "$OtherPassword\r"
    expect "$ "
}
"New Password: " {
 ## after authenticating, my old password expired need to change it now
    send ${NewPassword}\r
    expect "Re-enter new Password: "
    send ${NewPassword}\r
    expect "$ "
}
}
EOF
)
done
#/bin/bash
NewPassword=“CCCC”
OtherPassword=“ffffffff”
因为我在'cat x.txt`
做

/opt/csw/bin/expect-f条款在
expect
事件中的顺序;内部匹配器将按照指定的顺序尝试不同的匹配规则。这在这里很重要,因为
密码:
的规则也匹配
新密码:
将匹配的内容,因此优先于它


交换两个子句或重写它们,使它们不能同时匹配相同的输入文本(可能包括换行符或更改为使用锚定正则表达式)。交换它们要容易得多。

除了Donal令人信服的建议之外,还有几个注意事项:

  • 在shell部分中,不要阅读带有
    的行
  • 使用流程替换和
    cat
    以及here文档是过分的:您只需要here文档
  • 使用
    exp\u continue
    处理在看到提示之前发生的异常情况
  • 退出您的登录会话,并期望看到它关闭,这样您就不必等待超时
  • #/bin/bash
    NewPassword=“CCCC”
    OtherPassword=“ffffffff”
    读服务器时;做
    
    /opt/csw/bin/expect为什么我问这个问题会被责骂。
    #!/bin/bash
    NewPassword="cccccc"
    OtherPassword="ffffffff"
    
    while read server; do
        /opt/csw/bin/expect << EOF
            spawn ssh -o StrictHostKeyChecking=no adminuser@$server
            expect "Password: "
            send "$NewPassword\r"
            set count 0
            expect {
                "New Password: " {
                    ## after authenticating, my old password expired need to change it now
                    send ${NewPassword}\r
                    expect "Re-enter new Password: "
                    send ${NewPassword}\r
                    exp_continue
                }
                "Password: " {
                    if {[incr count] == 2} {
                        error "neither new password nor old password worked for $server"
                    }
                    ## The new password did not work 
                    send "$OtherPassword\r"
                    exp_continue
                }
                "$ "
            }
            send "uname -a\r"
            expect "$ "
            send "exit\r"
            expect eof
    EOF
    done < x.txt