46-使用传参输入用户名并自动生成随机密码
randpass模块参见 https://www.cnblogs.com/hejianping/p/10881293.html
脚本名字:adduser.py
import subprocess
import sys
from randpass import gen_pass
def adduser(username, password, fname):
data = """user information:
%s: %s
"""
subprocess.call('useradd %s' % username, shell=True)
subprocess.call(
'echo %s | passwd --stdin %s' % (password, username),
shell = True
)
with open(fname, 'a') as fobj:
fobj.write(data % (username, password))
if __name__ == '__main__':
username = sys.argv[1]
password = gen_pass()
adduser(username,password,'./user.txt')
执行脚本:
# python3 adduser.py boy
# python3 adduser.py girl
输出结果: