我正在编写一个Python程序来管理FreeRadius服务器中的用户帐户。如果我不想在输出文件(users.conf)中添加任何重复的帐户,如何更改adduser命令中的代码创建单个帐户)和au命令(用于导入文件以创建多个帐户)?
用户名由5位数字组成,在行的第一个单词中。我不希望在创建用户帐户后在输出文件中重复该用户名。将其更改为由csv文件导入后,如何检查createuserentry.get()是否与现有的用户名重复 users.conf,如果添加用户时发现重复,则停止该过程?
当我导入csv文件以创建多个帐户时,还如何通过与users.conf比较来检查csv文件中的重复用户名并防止将重复的用户名添加到users.conf中?
56185用户密码==“ 56185”
def adduser():
with open('C:/FreeRADIUS.net/etc/raddb/users.conf', "r+") as fh:
completed_lines = set()
for line in open('C:/FreeRADIUS.net/etc/raddb/users.conf', "r"):
if createuserentry.get() not in completed_lines:
fh.write('\n')
fh.write(createuserentry.get() + " " + "User-Password == " + '"' + createuserpwd.get() + '"')
fh.write('\n')
fh.close()
tkMessageBox.showinfo("success", "Account created")
else: continue
fh.close()
tkMessageBox.showinfo("Failure", "Username is duplicated")
def au():
au.filename = tkFileDialog.askopenfilename(initialdir = "/",title = "Select file to open",filetypes = (("csv","*.csv"),("all files","*.*")))
print(au.filename)
with open(au.filename) as input_file:
reader = csv.DictReader(input_file)
f = open('C:/FreeRADIUS.net/etc/raddb/users.conf', "a+")
seen = set()
for row in reader:
rows = tuple(row)
f.write(row.get("username") + " " + "User-Password == " + '"' + row.get("password") + '"' + "\n")
f.close()
tkMessageBox.showinfo("success", "Accounts created and all duplicate items are ignored")
def deleteuser():
with open('C:/FreeRADIUS.net/etc/raddb/users.conf', "r") as fh:
lines = fh.readlines()
with open('C:/FreeRADIUS.net/etc/raddb/users.conf', "w") as fh:
for line in lines:
if deleteuserentry.get() in line and deleteuserpwd.get() == "Pa$$w0rd123" :
tkMessageBox.showinfo("success", "Account Deleted")
end()
def du():
du.filename = tkFileDialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("csv","*.csv"),("all files","*.*")))
print(du.filename)
with open(du.filename) as input_file:
reader = csv.DictReader(input_file)
f = open('C:/FreeRADIUS.net/etc/raddb/users.conf', 'r+')
for row in reader:
rows = tuple(row)
if row.get("delete") == "y":
f.write(row + '\n')
f.close()
tkMessageBox.showinfo("success", "Accounts deleted")