算是一个实用的例子,定制系统任务,并将随机密码上传至日志服务器,实现定期修改密码;

部分代码:

 1 #!/usr/bin/env python
2 #coding:utf-8
3 import random,string,os,pexpect,time,re
4 def passwd_Create(): #生成随机密码
5 all_choice = string.ascii_letters+string.digits
6 passwd = ''
7 for i in range(8):
8 passwd += random.choice(all_choice)
9 return passwd
10
11 def passwd_Change(name,pwd): #更改密码
12 child = pexpect.spawn('passwd '+name)
13 index = child.expect(['New password',pexpect.EOF,pexpect.TIMEOUT])
14 if index == 0 :
15 child.sendline(pwd)
16 time.sleep(2)
17 child.sendline(pwd)
18 time.sleep(2)
19 child.close(force=True)
20 else:
21 print "expect ERROR"
22 child.close(force=True)
23
24 def log_Note(name,key): #记录日志
25 with open('/var/log/passwd','a+') as log:
26 counts = time.ctime()+" ["+name+"]"+" password is"+" ["+key+"]"+"\n"
27 log.write(counts)
28
29 def checkPw(passwd): #检测密码的强度
30 plen = len(passwd)
31 print plen
32 chpw1 = re.compile(r'.*[A-Z]+.*')
33 chpw2 = re.compile(r'.*[a-z]+.*')
34 chpw3 = re.compile(r'.*\d{1,}.*')
35 chresult1 = chpw1.findall(passwd)
36 print "匹配大写字符: ",chresult1
37 chresult2 = chpw2.findall(passwd)
38 print "小写字符: ",chresult2
39 chresult3 = chpw3.findall(passwd)
40 print "至少一个数字: ",chresult3
41
42 if chresult1 and chresult2 and chresult3:
43 print "You will change passwd use this password"
44 return 0
45 else:
46 print "password is not safety,will generate a safety passwd"
47 return 1
48
49 users = ['root','tom','alice'] #系统用户列表
50
51 if __name__ == "__main__":
52 for i in range(len(users)):
53 a = 1
54 while a != 0 :
55 keys = passwd_Create()
56 a = checkPw(keys)
57 passwd_Change(users[i],keys)
58 log_Note(users[i],keys)

Python实现自动更改系统用户密码,生成随机密码的更多相关文章

  1. linux设置系统用户密码

    目录 一:系统用户密码 1.设置用户密码 一:系统用户密码 1.设置用户密码 1.交互式方法 passwd [用户名] 2.免交互式 echo [设置密码] | passwd --stdin [用户名 ...

  2. [转] 关于linux下通过shell命令(自动)修改用户密码

    关于linux下通过shell命令(自动)修改用户密码 2012-04-23 18:47:39 分类: 原文地址:关于linux下(自动)修改用户密码 作者:ubuntuer 本文章总结了如何手动.自 ...

  3. web更改AD用户密码

    web更改AD用户密码 #web更改AD密码 #网站配置 绑定域名ad.test.cn 功能,更改AD用户密码 #参考http://bbs.51cto.com/thread-1379675-1.htm ...

  4. 重设windows10中的sub linux系统用户密码

    原文:重设windows10中的sub linux系统用户密码 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/haiyoung/article/detai ...

  5. Centos7下关于系统用户密码规则-运维笔记

    针对Centos7下的系统用户的密码规则复杂度的设置,处于安全考虑,说明如下: 一.设置密码规则 1)密码长度.有效期 /etc/login.defs文件是当创建用户时的一些规划,比如创建用户时,是否 ...

  6. SQL Server如何更改系统用户dbo的所属账号

    在SQL Server的每个数据库中都有一个dbo系统用户,dbo是系统默认创建的,无法被删除,如下: dbo在内部其实是绑定了一个SQL Server账号的,可以通过其属性查看Login name, ...

  7. VMware虚拟化NSX-Manager命令行更改admin用户密码

    1.1    登录到NSX-Manager命令行界面,输入用户名和密码登录到用户模式 Log in to the vSphere Client and select an NSX virtual ap ...

  8. changepassword.c 0.9:一个通过WEB界面更改LINUX用户密码的程序

    偶然看到一个用C语言写的CGI程序,可以以WEB界面(无需单独再写HTML)的方式修改用户自己的密码.该程序具有同时修改samba及squid密码的能力. 步骤: 1.下载并解压,并读一下README ...

  9. Centos6下关于系统用户密码规则-运维笔记

    随着linux使用的普遍,对于linux用户以及系统的安全要求越来越高,而用户密码复杂程度是系统安全性高低的首要体现.因此如何对linux下用户的密码进行规则限制,以保证用户必须使用复杂的密码,杜绝用 ...

随机推荐

  1. JAVA判断是否是Ajax请求

    /** * 是否是Ajax异步请求 * * @param request */ public static boolean isAjaxRequest(HttpServletRequest reque ...

  2. ARTS Week 18

    Algorithm 本周的 LeetCode 题目为 55. 跳跃游戏 给定一个非负整数数组 nums, 你最初位于数组的 第一个下标 .数组中的每个元素代表你在该位置可以跳跃的最大长度.判断你是否能 ...

  3. 【LeetCode】744. Find Smallest Letter Greater Than Target 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性扫描 二分查找 日期 题目地址:https:// ...

  4. Network (poj1144)

    A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting se ...

  5. 【LeetCode】804. Unique Morse Code Words 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 set + map set + 字典 日期 题目地 ...

  6. Codeforces A. Orchestra

    A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. Understanding and Improving Fast Adversarial Training

    目录 概 主要内容 Random Step的作用 线性性质 gradient alignment 代码 Andriushchenko M. and Flammarion N. Understandin ...

  8. Robust Pre-Training by Adversarial Contrastive Learning

    目录 概 主要内容 代码 Jiang Z., Chen T., Chen T. & Wang Z. Robust Pre-Training by Adversarial Contrastive ...

  9. Unsupervised Feature Learning via Non-Parametric Instance Discrimination

    目录 概 主要内容 Wu Z., Xiong Y., Yu S. & Lin D. Unsupervised Feature Learning via Non-Parametric Insta ...

  10. Gradient-based Hyperparameter Optimization through Reversible Learning

    目录 概 主要内容 算法 finite precision arithmic 实验 Maclaurin D, Duvenaud D, Adams R P, et al. Gradient-based ...