标签:else current 删除用户 user microsoft 一个 use print size
需求:
批量新建10个测试用户,并且让其密码随机,把用户名和密码写入一个文件,并有创建时间和创建者
#!/usr/bin/python # -*- coding: utf-8 -*- import os import random import time import io def deluser(nums): ‘‘‘删除用户测试用户‘‘‘ for i in range(nums): username = ‘stu‘ + str(i) linux_cmd = ‘userdel -r {username}‘.format(username=username) cmd_stat = os.system(linux_cmd) if cmd_stat == 0: print(username + " userdel: OK") else: print(username + " userdel: FAIL") def create_user(nums, record_adduser): ‘‘‘添加用户测试用户‘‘‘ if isinstance(nums, str): nums = int(nums) symbol = ‘1234567890‘ if not os.path.exists(os.path.dirname(record_adduser)): # 没有这个目录创建 os.mkdir(os.path.dirname(record_adduser)) # 这个文件直接打开,没有就创建 f = io.open(record_adduser, ‘a+‘, encoding=‘utf-8‘) for i in range(nums): passwd = ‘‘.join(random.sample(symbol, 6)) username = ‘stu‘ + str(i) linux_cmd = ‘useradd {username} && echo "{passwd}" | passwd {username} --stdin{linesep}‘.format(username=username, passwd=passwd, linesep=os.linesep) current_user = os.popen(‘whoami‘).read().strip() cmd_stat = os.system(linux_cmd) tmp = str(time.strftime(‘%Y-%m-%d %H:%M ‘, time.localtime()) + ‘ ‘ + current_user + ‘ ‘ + linux_cmd + ‘ {stat}‘) if cmd_stat == 0: print(username + " useradd: OK") tmp = tmp.format(stat=‘OK‘).decode(‘utf-8‘) # 创建成功写一句 f.write(tmp) else: print(username + " useradd: FAIL") tmp = tmp.format(stat=‘FAIL‘).decode(‘utf-8‘) f.write(tmp) # f.flush() f.close() if __name__ == ‘__main__‘: record_adduser = ‘/root/adduser{sep}useradd.log‘.format(sep=os.path.sep) create_user(10, record_adduser) # deluser(10) # python 2 下,str 是 bytes类型,文件通过 io模块打开
标签:else current 删除用户 user microsoft 一个 use print size
原文地址:http://www.cnblogs.com/2bjiujiu/p/7898857.html