Shell修改密码为原密码

  密码到期后更改密码为原密码,服务器多时工作量巨大,编写脚本完成。

  登陆后修改提示符为: 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 {2wyx@WYX}
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 $pass
exec_cmd "LANG=C;export LANG"

exec_cmd passwd
change_pass $pass $pass_1
exec_cmd passwd
change_pass $pass_1 $pass_2
exec_cmd passwd
change_pass $pass_2 $pass_3
exec_cmd passwd
change_pass $pass_3 $pass_4
exec_cmd passwd
change_pass $pass_4 $pass_5
exec_cmd passwd
change_pass $pass_5 $new
    
logout

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。