#coding=utf8
import pwd
import grp
import sys
from _utils.patrol2 import run_cmd info=None
try:
info=pwd.getpwuid(uid)
except Exception as e:
pass if info:
print 'uid已存在'
sys.exit(1) try:
info=pwd.getpwnam(user)
except Exception as e:
pass
if info:
print '用户已存在'
sys.exit(1) try:
info=grp.getgrgid(gid)
except Exception as e:
pass if info:
print 'gid已存在'
sys.exit(1) try:
info = grp.getgrnam(group)
except Exception as e:
pass if info:
print 'group名称已存在'
sys.exit(1) def run(cmd):
code, res = run_cmd(cmd)
if code:
print res
sys.exit(1) try:
run('groupadd -g {} {}'.format(gid,user))
print '创建用户组成功' run('useradd -u {} -d /var/mqm -g {} -m {}'.format(uid,group,user))
print '创建用户并添加用户组成功' run('echo "{}" |passwd --stdin {}'.format(password,user))
print '修改用户密码成功'
except Exception as e:
print e
sys.exit(1)