1.Python代码
[root@njmon python]# cat username.py
#!/usr/bin/env python
import os
import re
def get_username_func():
global username
global password
file = open("/home/python/username.txt", "r")
li = []
for line in file.readlines():
a = line.strip("/n")
print('username is %s' % line.strip("/n").split(",")[0])
print('password is %s' % line.strip("/n").split(",")[1])
username = line.strip("/n").split(",")[0]
print(username)
password = line.strip("/n").split(",")[1]
print(password)
create_user_func(username,password)
file.close()
def create_user_func(username,password):
with open('/etc/passwd', 'r') as fd:
for line in fd:
matchusername = re.search(r'%s' % username, line, re.I)
if not matchusername:
os.system('useradd %s -p %s' % (username,password))
print('%s is create successfully' % username)
else:
print('%s is already exists.' % username)
def main():
get_username_func()
if __name__ == "__main__":
main()
2.配置文件
[root@njmon python]# cat username.txt
jack,passw0rd
aaron,passw0rd
3.执行效果,如果username存在,则不创建
[root@njmon python]# python username.py
username is jack
password is passw0rd
jack
passw0rd
jack is create successfully
username is aaron
password is passw0rd
aaron
passw0rd
useradd: user 'aaron' already exists
aaron is create successfully
版权声明:本文为博主原创文章,未经博主允许不得转载。
Python