# -*- coding: utf-8 -*-
# Notes: This script is to create a user on the linux server, The random password storage path is: /root/mima.txt
# By: dugushubo
# Time: 2021-07-06

import string
import random
import subprocess
import os

def gen_pass():
    pass_list = list(string.ascii_letters + string.digits)
    password =[]
    result = ''
    for i in range(8):
        password.append(random.choice(pass_list))
        result = ''.join(password)
    return result

def create_user(username, password, filename):
    if os.system('id %s &> /dev/null' % username) == 0:
        print('################## 该用户已存在,请重试!################')
        exit()

    subprocess.call('useradd %s' % username, shell=True)
    subprocess.call('echo %s | passwd --stdin %s' % (password, username), shell=True)

    with open(filename,'a') as fobj:
        fobj.write('账号: %s 密码: %s \n' %(username,password))


if __name__ == '__main__':
    username = raw_input('请输入需要创建的用户 >')
    password = gen_pass()
    create_user(username,password,'/root/mima.txt')

  

相关文章:

  • 2022-12-23
  • 2021-08-22
  • 2022-12-23
  • 2021-10-20
  • 2021-12-03
猜你喜欢
  • 2022-12-23
  • 2021-06-30
  • 2021-08-28
  • 2021-11-23
  • 2021-12-09
  • 2021-07-02
  • 2021-10-06
相关资源
相似解决方案