- 论坛徽章:
- 16
|
本帖最后由 expert1 于 2011-09-15 14:14 编辑
自动改密码- #!/usr/bin/python
- # -*- coding=utf-8 -*-
- # author : xiaofu68@live.cn
- # Usage: change user's password automatically.As usual,you want to change root's password, you may do it as the following:
- # 1.open terminal ,and then type "password root "
- # 2. type new password twice.
- #but run this chg_pwd_auto.py ,you just need to type "python chg_pwd_auto.py root "! Enjoy it!
- # Dont't forget to you have to install "pexpect" modules ,on Centos platform ,just run "yum -y install pexpect "
- import pexpect
- import sys
- import string
- import time
- from random import Random
- def genpwd():
- pwdchars=string.letters + string.digits +'~!@#$%^&*-_=+?' # password seed ,change it yourself.
- pwdlength=16
- return ''.join(Random().sample(pwdchars, pwdlength))
- def changepwd(user,newpwd):
- passwd=pexpect.spawn("passwd %s" % user)
- passwd.logfile = sys.stdout
- for repeat in xrange(2):
- passwd.expect("password: ")
- passwd.delaybeforesend = 0.07
- passwd.sendline(newpwd)
-
- time.sleep(0.1)
- def main():
- user=sys.argv[1]
- newpwd=genpwd()
- print "change password for %s" %user
- print "newpwd:%s" % newpwd
- changepwd(user,newpwd)
- if __name__ == "__main__":
- main()
复制代码 |
|