SoFunction
Updated on 2024-10-30

Analyze the process of creating a new user and generating a random password based on Python.

Note: This code is executed under Linux, windows can also be used to add the user password command changed to windows on the ok!

Creating a new user and generating random passwords with Python

import passwd_name as pn  #Import Randomized Name Password Module
import os

f = open("/tmp/","w")  switches the user name、Passwords are written to this file

for i in range(0,3):  # Add 3 users
  username=pn.random_name()
  ("useradd %s" %username)
  passwd = pn.random_passwd()
  ("echo %s | passwd --stdin %s" %(passwd,username))
  ("uesrname:%s password:%s\n" %(username,passwd))
  
()

("mail -s 'send user mail' root < /tmp/")  #Finally the fileE-maildo sth (for sb)rootsubscribers

Generate random name, random password module passwd_name.py

import string
from random import choice

passwordrange=string.ascii_letters+  #Passwords include upper and lower case numbers
usernamerange=string.ascii_uppercase  #Names in capital letters only
def random_passwd(num=8):  # Passwords take eight digits by default and can be customized
  letter=""
  for i in range(num):
    letter +=choice(passwordrange)
  print(letter)
  return letter

def random_name(num=5):  #Names take eight digits by default and can be customized
  letter=""
  for i in range(num):
    letter +=choice(usernamerange)
  print(letter)
  return letter  
if __name__=="__main__":
  random_passwd();
  random_name();

This is the whole content of this article.