import random
import os
import string
allchoice = string.letters + string.digits
"""
邮件正文,后面展现string.Template()的用法
"""
content = """your acccount is created
username is $user
password is $pwd"""
"""
8位随机密码生成
"""
def pwdgen(num = 8):
pwd = ''
for i in range(num):
pwd += random.choice(allchoice)
return pwd
if __name__=='__main__':
password = pwdgen()
username = raw_input('input your name:')
os.system('useradd %s' % username)
os.system('echo %s | passwd --stdin %s' % (password,username))
t = string.Template(content)
os.system("echo '%s' | mail -s 'create user' root" % t.substitute(user=username,pwd=password))