1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| import tkinter as tk from tkinter import messagebox import paramiko
HOST='192.168.10.114' USERNAME='root' PASSWORD='1'
def run_system_command(command): ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh.connect(HOST,username=USERNAME,password=PASSWORD) stdin,stdout,stderr=ssh.exec_command(command) output=stdout.read().decode('utf-8') error=stderr.read().decode('utf-8') return output.strip(),error.strip() except Exception as e: return "",str(e) finally: ssh.close()
def get_nginx_status(): output,error=run_system_command("systemctl is-active nginx") if error: return "未知" return output.strip()
def update_status_label(): status=get_nginx_status() status_label.config(text=f"nginx状态:{status}") root.after(5000,update_status_label)
def start_nginx(): output,error=run_system_command('systemctl start nginx') if error: messagebox.showerror("错误",f"启动nginx失败:{error}") else: messagebox.showinfo("成功","nginx已启动成功") update_status_label()
def stop_nginx(): output,error=run_system_command('systemctl stop nginx') if error: messagebox.showerror("错误",f"停止nginx失败:{error}") else: messagebox.showinfo("成功","nginx已停止成功") update_status_label()
def restart_nginx(): output,error=run_system_command('systemctl restart nginx') if error: messagebox.showerror("错误",f"重启nginx失败:{error}") else: messagebox.showinfo("成功","nginx已重启成功") update_status_label()
def view_nginx_logs(): output,error=run_system_command('tail -n 100 /var/log/nginx/access.log') if error: messagebox.showerror("错误",f"查看日志失败:{error}") else: logs_window=tk.Toplevel(root) logs_window.title('nginx日志') logs_text=tk.Text(logs_window,wrap='word') logs_text.insert(tk.END,output) logs_text.pack(fill='both',expand=True)
root=tk.Tk()
root.title("nginx管理工具")
status_label=tk.Label(root,text="nginx状态:正在检测",font=('Arial',12))
status_label.pack(padx=10,pady=10)
start_button=tk.Button(root,text="启动nginx",command=start_nginx) start_button.pack(padx=10,pady=10)
stop_button=tk.Button(root,text="停止nginx",command=stop_nginx) stop_button.pack(padx=10,pady=10)
restart_button=tk.Button(root,text="重启nginx",command=restart_nginx) restart_button.pack(padx=10,pady=10)
logs_button=tk.Button(root,text="查看nginx日志",command=view_nginx_logs) logs_button.pack(padx=10,pady=10) update_status_label()
root.mainloop()
|