標籤:
密碼到期後更改密碼為原密碼,伺服器多時工作量巨大,編寫指令碼完成。
登陸後修改提示符為: doraemon# 方便expect匹配
修改LANG=C,提示為英文防止幹擾:
#!/usr/bin/expect################################################################ 串連遠程主機proc do_login {passwd} { set timeout 10 set done 1 set timeout_case 0 set ps1 {PS1="doraemon#";export PS1} while {$done<3} { expect { *assword* { send $passwd\r incr done } \[$%>#] { set done 5 send $ps1\r\r break } timeout { set done 1 switch -- $timeout_case { 1 { send_user "try again ...\n" } 2 { exit 2 expect eof } } incr timeout_case } } } if {$done==3} { exit 3 expect eof }}################################################################ 執行命令proc exec_cmd {cmd} { expect -re "doraemon#$" send_user "\$cmd: $cmd\n" send $cmd\r}################################################################ 退出proc logout {} { expect -re "doraemon#$" send exit\r expect eof}################################################################ 執行命令proc change_pass {curr cmd} { expect { -re "current|existing|exist|old|Old" { send $curr\r exp_continue } -re "New|new" { send $cmd\r exp_continue } -re "successfully|uthentication|changed" { send \r } }}###############################################################if {$argc < 4} { send_user "Usage:$argv0 user pass ip newpass\n" exit 1}set user [lindex $argv 0]set pass [lindex $argv 1]set ip [lindex $argv 2]set new [lindex $argv 3]set pass_1 {1pqz!PAZ}set pass_2 {[email protected]}set pass_3 {3hwd#HWC}set pass_4 {4zxv$ZXV}set pass_5 {5nmb%NMB}spawn ssh -o StrictHostKeyChecking=no -l $user $ip do_login $passexec_cmd "LANG=C;export LANG"exec_cmd passwdchange_pass $pass $pass_1exec_cmd passwdchange_pass $pass_1 $pass_2exec_cmd passwdchange_pass $pass_2 $pass_3exec_cmd passwdchange_pass $pass_3 $pass_4exec_cmd passwdchange_pass $pass_4 $pass_5exec_cmd passwdchange_pass $pass_5 $new logout
Shell修改密碼為原密碼