pythonユーザーを作成し、パスワードを設定し、ユーザー情報をファイルに保存します.

5407 ワード

#!/root/kl/bin/python3
# author:kl
#     ,    ,            
import subprocess
import sys
from random import choice
from string import ascii_letters, digits

all_chs = ascii_letters + digits

def randpass(n=8):
    result = ''
    for i in range(n):
        ch = choice(all_chs)
        result += ch
    return result

def adduser(uname, passwd, fname):
    #          
    result = subprocess.run('id %s &> /dev/null' % uname, shell=True)
    if result.returncode == 0:
        print('     !')
        return
        #     ,    
    subprocess.run('useradd %s' % uname, shell=True)
    subprocess.run('echo %s |passwd --stdin %s' % (passwd, uname), shell=True)
    #           
    with    open(fname, 'a') as fobj:
        fobj.write('   :%s
:%s
'
% (uname, passwd)) if __name__ == '__main__': uname = sys.argv[1] fname = sys.argv[2] passwd = randpass() adduser(uname, passwd, fname) # : python aduser.py