-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnginx-manager-GUI.py
More file actions
142 lines (120 loc) · 5.31 KB
/
nginx-manager-GUI.py
File metadata and controls
142 lines (120 loc) · 5.31 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Nginx 服务远程管理图形界面工具
功能描述:
本脚本提供基于 Tkinter 的图形用户界面,用于远程管理 Nginx 服务。
通过 SSH 连接到远程服务器,实现 Nginx 服务的启动、停止、重启操作,
并提供实时状态监控和日志查看功能,为运维人员提供直观便捷的服务管理工具。
技术实现:
- 使用 Tkinter 构建桌面图形用户界面
- 使用 paramiko 库建立 SSH 连接和远程命令执行
- 采用 systemctl 命令管理 systemd 服务
- 使用定时器实现状态自动刷新
- 通过消息框和子窗口提供用户交互
界面组件设计:
1. 状态标签 - 实时显示 Nginx 服务状态
2. 启动按钮 - 执行 systemctl start nginx 命令
3. 停止按钮 - 执行 systemctl stop nginx 命令
4. 重启按钮 - 执行 systemctl restart nginx 命令
5. 日志查看按钮 - 显示 Nginx 访问日志
6. 操作消息框 - 显示操作成功或失败信息
"""
import tkinter as tk
from tkinter import messagebox
import paramiko
HOST = '172.16.183.80'
USERNAME = 'root'
PASSWD = '111111'
# ssh执行命令
def run_system_command(command):
ssh = paramiko.SSHClient()
# 自动接受远程主机host key
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(HOST, username=USERNAME, password=PASSWD)
stdin, stdout, stderr = ssh.exec_command(command)
output = stdout.read().decode()
error = stderr.read().decode()
return output.strip(), error.strip()
except Exception as e:
return "", str(e)
finally:
ssh.close()
# 获取nginx服务状态
def get_nginx_status():
output, error = run_system_command('systemctl is-active nginx')
if error:
return "Status unknown"
return output.strip()
# 自动更新nginx状态的标签
def update_status_label():
status = get_nginx_status()
# 动态更新状态标签的文本。status_label是一个全局变量,所以可以直接在函数里使用
status_label.config(text=f"Nginx Status: {status}")
# tkinter定时器函数,每5秒再次调用函数
root.after(5000, update_status_label)
# 启动nginx服务
def start_nginx():
output, error = run_system_command("systemctl start nginx")
if error:
# 定义弹窗
messagebox.showerror("Error", f"Failed to start nginx: {error}")
else:
messagebox.showinfo("Success", f"Nginx has been started")
update_status_label()
# 停止nginx服务
def stop_nginx():
output, error = run_system_command("systemctl stop nginx")
if error:
messagebox.showerror("Error", f"Failed to stop nginx: {error}")
else:
messagebox.showinfo("Success", f"Nginx has been stopped")
update_status_label()
# 重启nginx
def restart_nginx():
output, error = run_system_command("systemctl restart nginx")
if error:
messagebox.showerror("Error", f"Failed to restart nginx: {error}")
else:
messagebox.showinfo("Success", f"Nginx has been restarted")
update_status_label()
# 查看nginx日志
def view_nginx_logs():
output, error = run_system_command("tail -n 100 /var/log/nginx/access.log")
if error:
messagebox.showerror("Error", f"Failed to get nginx log: {error}")
else:
# 创建文本框子窗口,把日志放进去
logs_window = tk.Toplevel(root)
logs_window.title("Nginx Log")
# 在这个窗口中创建一个文本框 (Text),wrap='word' 意味着在文本过长时会自动换行。
logs_text = tk.Text(logs_window, wrap='word')
# 将 Nginx 日志(即 output)插入到文本框中,tk.END 表示将内容插入到文本框的末尾。
logs_text.insert(tk.END, output)
# 将文本框布局到窗口中,fill='both' 表示文本框会填满窗口的水平和垂直方向,expand=True 文本框会随着窗口的大小变化而扩展。
logs_text.pack(fill='both', expand=True)
if __name__ == '__main__':
# 创建一个顶层窗口对象 root,这是整个图形界面的主窗口。
root = tk.Tk()
root.title("Nginx manager")
# 显示nginx状态的标签
status_label = tk.Label(root, text="Nginx Status: Checking...", font=('Arial', 12))
# pack() 是用来布局组件的方法之一,它把标签添加到窗口中并设置布局
# padx=10, pady=10: 这是为标签设置的内边距,padx 是水平方向的间距,pady 是垂直方向的间距,值为 10,表示标签与窗口边缘或其他组件之间有 10 个像素的间距。
status_label.pack(padx=10, pady=10)
# 创建启动按钮
start_button = tk.Button(root, text="Start Nginx", command=start_nginx)
start_button.pack(padx=10,pady=10)
#创建停止按钮
stop_button = tk.Button(root, text="Stop Nginx", command=stop_nginx)
stop_button.pack(padx=10,pady=10)
#创建重启按钮
restart_button = tk.Button(root, text="Restart Nginx", command=restart_nginx)
restart_button.pack(padx=10,pady=10)
# 查看日志按钮
nginx_log_button = tk.Button(root, text="Get Nginx Log", command=view_nginx_logs)
nginx_log_button.pack(padx=10,pady=10)
update_status_label()
# 启动 tkinter 的主事件循环,使应用程序保持运行并响应用户输入。
root.mainloop()