我有Solaris服务器,我不确定我是否将密码更改为特定帐户。要么我要成功进行身份验证,因为我已经更改了密码,或者我要使用旧密码进行身份验证,并且在我使用旧密码进行身份验证后会提示我更改密码,因为它已过期。
Warning: Your password has expired, please change it now.
New Password:
脚本将在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
答案 0 :(得分:2)
expect
条款中的条款顺序;内部匹配器按您指定的顺序尝试不同的匹配规则。这一点很重要,因为Password:
的规则也与New Password:
匹配的内容相匹配,因此优先于{{1}}。
交换两个子句或重写它们,以便它们不能同时匹配相同的输入文本(可能包括换行符或更改为使用锚定的正则表达式)。交换它们要容易得多。
答案 1 :(得分:1)
除了Donal的有说服力的建议之外,还有几点注意事项:
cat
exp_continue
和这里的文档太过分了:你只需要here-doc #!/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
来处理在看到提示$("#register-page").css({"display" : "none" });