Shell修改密码为原密码

时间:2022-09-09 15:32:05

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

  登陆后修改提示符为: 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

   测试如下:

 

[root@centos1 shcript]# ./change_passwd.expect 
Usage:./change_passwd.expect user pass ip newpass
[root@centos1 shcript]# ./change_passwd.expect root root123 127.0.0.1 root123
spawn ssh -o StrictHostKeyChecking=no -l root 127.0.0.1
root@127.0.0.1's password: 
Last login: Fri Aug 14 02:55:22 2015 from 127.0.0.1
[root@centos1 ~]# PS1="doraemon#";export PS1
doraemon#
doraemon#$cmd: LANG=C;export LANG
LANG=C;export LANG
doraemon#$cmd: passwd
passwd
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
doraemon#$cmd: passwd

doraemon#passwd
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
doraemon#$cmd: passwd
......