我正在编写一个Python程序来管理FreeRadius用户帐户。我可以在程序中添加新用户,但是在输入目标用户的用户名和密码后无法删除用户。另外,我无法更改用户密码。
我应该如何更改编码以删除用户并提供密码更改功能?
npm cache clear --force
预期输出:
实际输出:无效的语法
答案 0 :(得分:0)
您的代码存在以下语法问题:
with
语句在末尾缺少as fh
和:
还有其他问题:
w
; 未经测试的更正代码如下:
#!usr/bin/python
from Tkinter import *
import ttk
import os
import subprocess
import commands
main = Tk()
main.title("FreeRadius Windows Administration Controller GUI")
main.geometry('1280x800')
rows = 0
while rows < 50:
main.rowconfigure(rows, weight=1)
main.columnconfigure(rows, weight=1)
rows += 1
def startlog():
f = open('C:/FreeRADIUS.net/var/log/radius/radius.log', 'r')
file_contents = f.read()
txtarea_output.insert(INSERT, file_contents)
print (file_contents)
f.close()
def resetlog():
txtarea_output.delete(1.0,END)
def adduser():
fh = open('C:/FreeRADIUS.net/etc/raddb/users.conf', 'a')
fh.write("\n")
fh.write(createuserentry.get() + " " + "User-Password == " + '"' + createuserpwd.get() + '"')
fh.close()
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: continue
fh.write(line)
def cpwd():
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 changeuserpwd.get() in line:
fh.write(changepwduserentry.get() + " " + "User-Password == " + '"' + changepwdusernewpwd.get() + '"')
else:
fh.write(line)
nb = ttk.Notebook(main)
nb.grid(row=1, column=0, columnspan=40, rowspan=39, sticky='NESW')
page1 = ttk.Frame(nb)
nb.add(page1, text='User Account Management')
createuser = ttk.Label(page1, text="Create User").grid(column=0, row=0, sticky='W')
createusername = ttk.Label(page1, text="Enter new user's username:").grid(column=0, row=1, sticky='W')
createuserentry = ttk.Entry(page1)
createuserentry.grid(row=1, column=1, columnspan=2)
createuserpassword = ttk.Label(page1, text="Enter new user's password:").grid(column=0, row=2, sticky='W')
createuserpwd = ttk.Entry(page1)
createuserpwd.grid(row=2, column=1, columnspan=2)
createuserconfirm = Button(page1, text='Create this User', borderwidth=2, command=adduser)
createuserconfirm.grid(row=3, column=0, sticky='W')
ttk.Label(page1, text="").grid(column=0, row=4, sticky='W')
deleteteuser = ttk.Label(page1, text="Delete User").grid(column=0, row=5, sticky='W')
deleteusername = ttk.Label(page1, text="Enter current user's username:").grid(column=0, row=6, sticky='W')
deleteuserentry = ttk.Entry(page1)
deleteuserentry.grid(row=6, column=1, columnspan=2)
deleteuserpassword = ttk.Label(page1, text="Enter current user's password:").grid(column=0, row=7, sticky='W')
deleteuserpwd = ttk.Entry(page1)
deleteuserpwd.grid(row=7, column=1, columnspan=2)
deleteuserconfirm = Button(page1, text='Deletete this User', borderwidth=2, command=deleteuser)
deleteuserconfirm.grid(row=8, column=0, sticky='W')
ttk.Label(page1, text="").grid(column=0, row=9, sticky='W')
changepwd = ttk.Label(page1, text="Change User's password").grid(column=0, row=10, sticky='W')
changepwdusername = ttk.Label(page1, text="Enter user's username:").grid(column=0, row=11, sticky='W')
changepwduserentry = ttk.Entry(page1)
changepwduserentry.grid(row=11, column=1, columnspan=2)
changepwduserpassword = ttk.Label(page1, text="Enter user's current password:").grid(column=0, row=12, sticky='W')
changepwduserpwd = ttk.Entry(page1)
changepwduserpwd.grid(row=12, column=1, columnspan=2)
changepwdusernewpwd = ttk.Label(page1, text="Enter user's new password:").grid(column=0, row=13, sticky='W')
changepwdusernewpwd = ttk.Entry(page1)
changepwdusernewpwd.grid(row=13, column=1, columnspan=2)
changepwduserconfirm = Button(page1, text='Change Password', borderwidth=2, command = cpwd)
changepwduserconfirm.grid(row=14, column=0, sticky='W')