码迷,mamicode.com
首页 > 系统相关 > 详细

Shell修改密码为原密码

时间:2015-06-02 12:56:05      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

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

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

 

Shell修改密码为原密码

标签:

原文地址:http://www.cnblogs.com/wbjxxzx/p/4545956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!