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
| import paramiko import tkinter as tk from tkinter import scrolledtext,messagebox import time
HOST = '192.168.10.102' SSH_USER = 'root' SSH_PASSWORD = '1' SERVICE_NAME = "httpd"
match SERVICE_NAME: case 'httpd': GET_STATUS = f"systemctl is-active {SERVICE_NAME}" START_SERVICE = f"systemctl start {SERVICE_NAME}" STOP_SERVICE = f"systemctl stop {SERVICE_NAME}" case 'nginx': GET_STATUS = f"systemctl is-active {SERVICE_NAME}" START_SERVICE = f"systemctl start {SERVICE_NAME}" STOP_SERVICE = f"systemctl stop {SERVICE_NAME}" case 'tomcat': GET_STATUS = "ps -ef | grep tomcat | grep -v grep" START_SERVICE = "/usr/local/bin/catalina start" STOP_SERVICE = "/usr/local/bin/catalina stop"
def run_command(command): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(HOST,username=SSH_USER, password=SSH_PASSWORD) try: stdin,stdout,stderr=ssh.exec_command(command=command) output=stdout.read().decode("utf-8") error=stderr.read().decode("utf-8") if "Picked up JDK_JAVA_OPTIONS" in error: error = "" return output.strip(),error.strip()
except Exception as e: return f"command excute failed: {e}"
finally: ssh.close()
def get_service_status(): timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) output,error = run_command(GET_STATUS) if output == "active": status_text.insert(tk.END, f"{SERVICE_NAME} is running {timestamp}\n") else: status_text.insert(tk.END, f"{SERVICE_NAME} is stoped {timestamp}\n")
def start_service(): output,error = run_command(START_SERVICE) if error: status_text.insert(tk.END,f"{SERVICE_NAME} start failed:\n{error}\n") else: status_text.insert(tk.END,f"{SERVICE_NAME} start successful\n") get_service_status()
def stop_service(): output,error = run_command(STOP_SERVICE) if error: status_text.insert(tk.END,f"{SERVICE_NAME} stop failed:\n{error}\n") else: status_text.insert(tk.END,f"{SERVICE_NAME} stop successful\n") get_service_status()
def check_status(): get_service_status() root.after(5000,check_status)
root = tk.Tk() root.title("web service manager")
start_button = tk.Button(root,text="Start Service",command=start_service,width=20) start_button.grid(row=0,column=0,padx=10,pady=10)
stop_button = tk.Button(root,text="Stop Service",command=stop_service,width=20) stop_button.grid(row=0,column=1,padx=10,pady=10)
exit_button = tk.Button(root,text="Exit",command=root.quit,width=20) exit_button.grid(row=0,column=2,padx=10,pady=10)
status_text = scrolledtext.ScrolledText(root,height=20,width=60) status_text.grid(row=1,column=0,columnspan=3,padx=10,pady=10)
check_status() root.mainloop()
|