我正在编写一个Python程序来管理FreeRadius服务器中的用户帐户。如果我不想在输出文件中添加任何重复的帐户 (users.conf)。如何更改adduser命令(用于创建单个帐户)和au命令(用于导入文件以创建多个帐户)中的代码?
用户名在行的第一个单词中包含5位数字。我不希望在创建用户帐户后在输出文件中重复该用户名。
56185用户密码==“ 56185”
我不希望发生此输出:
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() and createuserpwd.set() 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:
fh.close()
tkMessageBox.showinfo("Failure", "Username is duplicated")
def au():
au.filename = tkFileDialog.askopenfilename(initialdir = "/",title = "Select file to open",filetypes = (("txt","*.txt"),("all files","*.*")))
print(au.filename)
completed_lines_hash = set()
with open('C:/FreeRADIUS.net/etc/raddb/users.conf', 'a') as output_file:
with open(au.filename, "r") as input_file:
for line in open(au.filename, "r"):
hashValue = hashlib.md5(line.rstrip().encode('utf+8')).hexdigest()
if hashValue not in completed_lines_hash:
output_file.write(line)
completed_lines_hash.add(hashValue)
output_file.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 = (("text document","*.txt"),("all files","*.*")))
print(du.filename)
with open(du.filename, "r") as input_file:
with open('C:/FreeRADIUS.net/etc/raddb/users.conf', 'w+') as output_file:
for line in input_file:
if "User" in line: continue
output_file.write()
output_file.close()
tkMessageBox.showinfo("success", "Accounts deleted")