def register(): username = input('username: '******'password: '******'$6$%s$' % randpass()) userdb[username] = cpasswd print(cpasswd) else: print('user is exsist or you have not enter anything')
def adduser(username, password, fname): data= """user information: %s:%s """ #subprocess.run('useradd %s' % username,shell=True) #3.0以前没有run只能用call 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 = randpass() adduser(username, password, '/tmp/user.txt') #python3 adduser.py john # #模拟栈操作 # stack = [] # # def push_it(): # item = input('item to push: ') # stack.append(item) # # def pop_it(): # if stack: # print('from stack poipped %s'% stack.pop()) # # def view_it(): # print(stack)
'id %s' % username, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) if result.returncode == 0: print('%s已存在' % username) return False # 函数遇到return就返回,不会再向下执行 # 创建用户,并设置密码 subprocess.run('useradd %s' % username, shell=True) subprocess.run( 'echo %s | passwd --stdin %s' % (password, username), shell=True ) # 写入用户信息到文件 info = """user info: username: %s password: %s """ % (username, password) with open(fname, 'a') as fobj: fobj.write(info) if __name__ == '__main__': username = sys.argv[1] password = randpass2.randpass() fname = '/tmp/users.txt' adduser(username, password, fname)
import sys import subprocess from randpass2 import randpass def add_user(user, passwd, fname): result = subprocess.run('id %s &> /dev/null' % user, shell=True) if result.returncode == 0: print('用户已存在') return subprocess.run('useradd %s' % user, shell=True) subprocess.run('echo %s | passwd --stdin %s' % (user, passwd), shell=True) info = """username: %s password: %s """ % (user, passwd) with open(fname) as fobj: fobj.write(info) if __name__ == '__main__': user = sys.argv[1] fname = sys.argv[2] passwd = randpass() add_user(user, passwd, fname)
# 判断用是否已存在 reuslt = subprocess.run( 'id %s &> /dev/null' % uname, shell=True ) if reuslt.returncode == 0: print('用户已存在') # 函数遇到return就结束了,不会再向下执行 return # 创建用户、设置密码 subprocess.run('useradd %s' % uname, shell=True) subprocess.run( 'echo %s | passwd --stdin %s' % (passwd, uname), shell=True ) # 用户名和密码写到文件中 info = '''用户名: %s 密码: %s ''' % (uname, passwd) with open(fname, 'a') as fobj: fobj.write(info) if __name__ == '__main__': uname = sys.argv[1] fname = sys.argv[2] passwd = randpass2.randpass() adduser(uname, passwd, fname) # python adduser.py zs /tmp/users.txt