期望脚本在“新密码”提示下结束且没有错误

时间:2018-02-22 03:43:42

标签: tcl expect

我有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

2 个答案:

答案 0 :(得分:2)

expect条款中的条款顺序;内部匹配器按您指定的顺序尝试不同的匹配规则。这一点很重要,因为Password:的规则也与New Password:匹配的内容相匹配,因此优先于{{1}}。

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

答案 1 :(得分:1)

除了Donal的有说服力的建议之外,还有几点注意事项:

  1. 在shell部分中,不要使用cat
  2. 读取行
  3. 使用流程替换和exp_continue 这里的文档太过分了:你只需要here-doc
  4. 使用#!/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 来处理在看到提示
  5. 之前发生的异常情况
  6. 退出您的登录会话并希望看到它关闭,这样您就不必等待超时。
  7. $("#register-page").css({"display" : "none"  });