1)使用python的標準庫 Tkinterpython
2)使用wxPythonmysql
3)使用Jython(能夠和 Java無縫集成)nginx
安裝tkinter庫sql
yum install -y tkinter tk-devel
建立1個空白的主窗口編程
#!/usr/bin/python # -*- coding: UTF-8 -*- import Tkinter # 建立一個主窗口 myWindow = Tkinter.Tk() # 進入消息循環 myWindow.mainloop()
寫一個服務啓動的圖形界面:centos
[root@cml10 python]# cat win2.py #!/usr/bin/python #_*_coding:utf-8_*_ import os from Tkinter import * # 導入 Tkinter 庫 def restart_server(): ip= listbox1.get(listbox1.curselection()) server= listbox2.get(listbox2.curselection()) os.system("sshpass-p 'redhat' ssh root@%s service %s restart" %(ip,server)) print"sshpass -p 'redhat' ssh root@%s service %s restart" %(ip,server) label_info["text"]="服務器(%s)正在重啓%s服務..."%(ip,server) mywin.update() def start_server(): ip= listbox1.get(listbox1.curselection()) server= listbox2.get(listbox2.curselection()) os.system("sshpass-p 'redhat' ssh root@%s service %s start" %(ip,server)) print"sshpass -p 'redhat' ssh root@%s service %s start" %(ip,server) label_info["text"]="武器器(%s)正在啓動%s服務..."%(ip,server) mywin.update() def stop_server(): ip = listbox1.get(listbox1.curselection()) server = listbox2.get(listbox2.curselection()) os.system("sshpass -p 'redhat' ssh root@%s service %s stop"%(ip,server)) print "sshpass -p 'redhat' ssh root@%s service %s stop"%(ip,server) label_info["text"]="武器器(%s)正在中止%s服務..."%(ip,server) mywin.update() mywin = Tk() # 建立窗口對象的背景色 mywin.title("server manager") mywin.geometry("700x400") #建立標籤 label1 = Label(mywin,text="服務器列表",font=("Arial",20),bg='yellow',fg='red') label1.grid(row=0,column=0) label2 = Label(mywin,text="服務名稱",font=("Arial",20),bg='blue',fg='red') label2.grid(row=0,column=1) label3 = Label(mywin,text="操做",font=("Arial",20),bg="blue",fg="red") label3.grid(row=0,column=2,columnspan=4) label_info =Label(mywin,text="",font=("Arial",20),bg='purple',fg='red') label_info.grid(row=2,column=0) #建立列表組件 listbox1 =Listbox(mywin,exportselection=False) listbox2 =Listbox(mywin,exportselection=False) #exportselection=False,是的選擇其餘的Listbox時,原來的選擇依然有效 #建立按鈕 button1 = Button(mywin,text="重啓服務",command=restart_server) button2 = Button(mywin,text="啓動服務",command=start_server) button3 = Button(mywin,text="中止服務",command=stop_server) #給列表組件添加數據 ips = [] for ip in range(128,150): # 第一個小部件插入數據 tmp= "192.168.5.%d" %ip ips.append(tmp) for ip in ips: listbox1.insert(0,ip) services =["nginx","mariadb","ftp","network","httpd","mysql"] for service in services: listbox2.insert(0,service) # 第二個小部件插入數據 #給listbox佈局 listbox1.grid(row=1,column=0,padx=20,pady=20,ipady=10) listbox2.grid(row=1,column=1,padx=20,pady=20) button1.grid(row=1,column=2,padx=10,pady=10) button2.grid(row=1,column=3,padx=10,pady=10) button3.grid(row=1,column=4,padx=10,pady=10) mywin.mainloop() # 進入消息循環
執行win2.py的效果圖:bash
[root@cml10 python]# python win2.py服務器
測試直接點擊啓動、中止、重啓服務:app
##centos-1主機地址爲192.168.5.129,且httpd服務沒啓動:ssh
[root@centos-1 ~]# service httpd status
httpd 已停
在圖形界面點擊192.168.5.129àhttpdà啓動服務
在centos-1機器上查看httpd的狀態:
[root@centos-1 ~]# service httpd status
httpd (pid 5206) 正在運行...
在點擊中止服務:
查看centos-1主機httpd狀態:
[root@centos-1 ~]# service httpd status
httpd 已停
點擊重啓:
查看centos-1主機httpd狀態:
[root@centos-1 ~]# service httpd status
httpd (pid 5635) 正在運行...