wxpython窗体小程序,按公司要求修改IP
#!/usr/bin/env python
# encoding: utf-8
'''
Created on 2017年5月22日
@author: pengdj
'''
import wx
import os
import socket
import fcntl
import struct
import re
import sys
import libvirt
import subprocess
class ZopleNoteBook(wx.Notebook):
def __init__(self, parent):
wx.Notebook.__init__(self, parent, -1,
size = (21,21), style = wx.BK_DEFAULT)
networkPanel = NetworkPanel(self)
aboutPanel = ablouPanel(self)
TaemPanel=TAEMPanel(self)
self.AddPage(aboutPanel, u'资源信息检测')
self.AddPage(networkPanel, u'修改IP地址')
#print TaemPanel._vm
if (TaemPanel._vm.flags != 1):
self.AddPage(TaemPanel, u'管理节点控制台')
class SettingDialog(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, u"太易管理工具", size = (600,900))
panel = wx.Panel(self)
#panel.SetBackgroundColour('#B3B2B3')
sizer = wx.BoxSizer(wx.HORIZONTAL)
nb = ZopleNoteBook(self)
self.noteBook = nb
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(sizer, 0, flag = wx.EXPAND)
mainSizer.AddSpacer(10)
mainSizer.Add(nb, 1, flag = wx.EXPAND)
sizer = wx.FlexGridSizer(cols = 2, hgap = 15, vgap = 20)
mainSizer.AddSpacer(10)
mainSizer.Add(sizer, 0, flag = wx.ALIGN_BOTTOM | wx.ALIGN_RIGHT)
self.SetSizer(CreateCenterSizer(mainSizer, 10))
def CreateCenterSizer(win, gap):
marginSizer = wx.FlexGridSizer(rows = 3, cols = 3)
marginSizer.AddGrowableCol(1)
marginSizer.AddGrowableRow(1)
for i in range(0,4):
marginSizer.AddSpacer(gap)
marginSizer.Add(win, 0, wx.EXPAND)
for i in range(0,4):
marginSizer.AddSpacer(gap)
return marginSizer
class NetworkPanel(wx.Panel):
def __init__(self, parent):
self._iface="br0"
self._gateway=os.popen("route -n|awk '/%s/{print $2}'|head -n 1" % self._iface).read().strip('\n')
self._localip=os.popen("ifconfig %s |grep -w inet|awk '{print $2}'"% self._iface).read().strip('\n')
#get_ip_address(self._iface)
self._dns=os.popen("awk '/nameserver/ {print $2}' /etc/resolv.conf |head -n 1").read().strip('\n')
wx.Panel.__init__(self, parent)
mainSizer = wx.BoxSizer(wx.VERTICAL)
midSizer = wx.BoxSizer(wx.HORIZONTAL)
self.local_info=u'''系统信息列表 \n\t\n\t管理节点:192.168.1.253\n\t本机IP:%s\n\t网关:%s\n\tDNS:%s\n\t子网掩码:255.255.255.0''' %(self._localip,self._gateway,self._dns)
self.infomation_label=wx.StaticText(self,-1,self.local_info)
staticbox = wx.StaticBox(self, -1, u"静态地址设置")
midSizer = wx.StaticBoxSizer(staticbox, wx.VERTICAL)
sizer = wx.FlexGridSizer(cols = 2, hgap = 10, vgap = 15)
sizer.AddGrowableCol(1)
sizer.Add(wx.StaticText(self, -1, u"管理节点IP:"), 0,
wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
self.TAEM_IP_INPUT = wx.TextCtrl(self)
sizer.Add(self.TAEM_IP_INPUT, 0, wx.EXPAND)
sizer.Add(wx.StaticText(self, -1, u"IP地址:"), 0,
wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
self.local_ip_input = wx.TextCtrl(self)
sizer.Add(self.local_ip_input, 0, wx.EXPAND)
sizer.Add(wx.StaticText(self, -1, u"网络掩码:"), 0,
wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
self.MASK_INPUT = wx.TextCtrl(self)
sizer.Add(self.MASK_INPUT, 0, wx.EXPAND)
sizer.Add(wx.StaticText(self, -1, u"网关:"), 0,
wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
self.GATEWAY_IP_INPUT = wx.TextCtrl(self)
sizer.Add(self.GATEWAY_IP_INPUT, 0, wx.EXPAND)
sizer.Add(wx.StaticText(self, -1, u"DNS:"), 0,
wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
self.DNS_INPUT = wx.TextCtrl(self)
sizer.Add(self.DNS_INPUT, 0, wx.EXPAND)
self.button_box=wx.BoxSizer(wx.HORIZONTAL)
self.btn_ok = wx.Button(self, wx.ID_OK, u'确定')
self.btn_cancel = wx.Button(self, wx.ID_CANCEL, u'取消')
self.button_box.Add(self.btn_ok, 0)
self.button_box.Add(self.btn_cancel,0)
self.Message_label=wx.StaticText(self,-1,'')
midSizer.Add(CreateCenterSizer(sizer, 10), 1, wx.EXPAND)
mainSizer.Add(self.infomation_label)
mainSizer.AddSpacer(5)
mainSizer.Add(midSizer, 0, wx.EXPAND)
mainSizer.Add(self.button_box,0)
mainSizer.Add(self.Message_label,0)
self.Bind(wx.EVT_BUTTON, self.OnClick_Modify, self.btn_ok)
self.Bind(wx.EVT_BUTTON, self.OnClicK_Quit, self.btn_cancel)
self.SetSizer(CreateCenterSizer(mainSizer, 10))
def OnSave(self):
pass
def OnClick_Modify(self,event):
if (not self.DNS_INPUT.GetValue()):
self._DNS=self.GATEWAY_IP_INPUT.GetValue()
else:
self._DNS=self.DNS_INPUT.GetValue()
if (not self.MASK_INPUT.GetValue()):
self._MASK="255.255.255.0"
else:
self._MASK=self.MASK_INPUT.GetValue()
self._LOCAL_IP=self.local_ip_input.GetValue()
self._GATEWAY=self.GATEWAY_IP_INPUT.GetValue()
self._TAEM_IP=self.TAEM_IP_INPUT.GetValue()
if ( self.local_ip_input.GetValue() and self.GATEWAY_IP_INPUT.GetValue() and self.TAEM_IP_INPUT.GetValue()):
while 1:
if (not ipFormatChk(self._TAEM_IP)):
self.Message_label.SetLabel(u"请输入正确的管理节点IP")
break
if (not ipFormatChk(self._LOCAL_IP)):
self.Message_label.SetLabel(u"请输入正确的本机IP")
break
if (not ipFormatChk(self._GATEWAY)):
self.Message_label.SetLabel(u"请输入正确的网关")
break
if (not ipFormatChk(self._DNS)):
self.Message_label.SetLabel(u"请输入正确的DNS地址或者不输入默认使用网关")
break
if (not ipFormatChk(self._MASK)):
self.Message_label.SetLabel(u"请输入正确的掩码地址或者不输入使用默认的255.255.255.0")
break
self.Message_label.SetLabel(u"参数正确")
Modify_Do(self._LOCAL_IP,self._TAEM_IP,self._GATEWAY,self._DNS,self._MASK)
sys.exit()
else:
self.Message_label.SetLabel(u"请讲必须添加的参数不全,管理节点IP,网关,本机IP")
def OnClicK_Quit(self,event):
sys.exit()
class ablouPanel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self, parent)
about_mainSizer = wx.BoxSizer(wx.VERTICAL)
sizer = wx.BoxSizer(wx.VERTICAL)
self.cpu_staticbox = wx.StaticBox(self, -1, u"cpu信息")
self.cpu_midSizer = wx.StaticBoxSizer(self.cpu_staticbox, wx.VERTICAL)
self.cpu_sizer = wx.FlexGridSizer(cols = 2, hgap = 5, vgap = 10)
self.cpu_sizer.AddGrowableCol(1)
self.cpu_sizer.Add(wx.StaticText(self, -1, u"CPU厂商:"), 0,
wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
self.cpu_chip = wx.StaticText(self,-1,os.popen("awk -F: '/model name/{print $NF}' /proc/cpuinfo |head -n 1").read().strip("\n"))
self.cpu_sizer.Add(self.cpu_chip, 0, wx.EXPAND)
self.cpu_sizer.Add(wx.StaticText(self, -1, u"cpu核数:"), 0,
wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
self.cpu_core= wx.StaticText(self,-1,os.popen("awk -F: '/cpu cores/{print $NF}' /proc/cpuinfo|head -n 1").read().strip("\n"))
self.cpu_sizer.Add(self.cpu_core, 0, wx.EXPAND)
self.cpu_sizer.Add(wx.StaticText(self, -1, u"CPU总线程数:"), 0,
wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
self.cpu_process= wx.StaticText(self,-1,os.popen("grep process /proc/cpuinfo |wc -l").read().strip("\n"))
self.cpu_sizer.Add(self.cpu_process, 0, wx.EXPAND)
self.cpu_sizer.Add(wx.StaticText(self, -1, u"当前主频:"), 0,
wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
self.cpu_curt_GHZ = wx.StaticText(self,-1,u'')
self.cpu_sizer.Add(self.cpu_curt_GHZ, 0, wx.EXPAND)
self.cpu_midSizer.Add(CreateCenterSizer(self.cpu_sizer, 10), 1, wx.EXPAND)
self.timer=wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self._cpu_label=wx.StaticText(self,1,u"CPU使用率:",size=(200,20))
self._cpu_label_stat=wx.StaticText(self,1,u"%usr %sys %iowait %idle",size=(200,20))
self._cpu_stat=wx.StaticText(self,1,"",size=(200,20))
self._cpu_air=wx.StaticText(self,1, u"CPU温度:",size=(200,20))
self._cpu_air_stat=wx.StaticText(self,1, u"",size=(200,20))
self._mem_label=wx.StaticText(self,1,u"内存使用率:",size=(200,20))
self._mem_stat=wx.StaticText(self,1,"",size=(200,20))
self._part_label=wx.StaticText(self,1,u"磁盘分使用情况:")
self._part_stat=wx.StaticText(self,1,u"",size=(200,200),style=wx.TE_MULTILINE|wx.TE_RICH2)
self._disk_label=wx.StaticText(self,1,u"磁盘读写情况:")
self._disk_stat=wx.StaticText(self,1,u"",size=(200,150),style=wx.TE_MULTILINE|wx.TE_RICH2)
sizer.Add(self._cpu_label, 1, wx.EXPAND)
sizer.Add(self._cpu_label_stat, 1, wx.EXPAND)
sizer.Add(self._cpu_stat, 1, wx.EXPAND)
sizer.Add(self._cpu_air, 1, wx.EXPAND)
sizer.Add(self._cpu_air_stat, 1, wx.EXPAND)
sizer.Add(self._mem_label, 1, wx.EXPAND)
sizer.Add(self._mem_stat, 1, wx.EXPAND)
about_staticbox = wx.StaticBox(self, -1, u"网络测试")
about_midSizer = wx.StaticBoxSizer(about_staticbox, wx.VERTICAL)
ping_sizer = wx.FlexGridSizer(cols = 2, hgap = 10, vgap = 15)
ping_sizer.AddGrowableCol(1)
self.Ping_IP = wx.TextCtrl(self,size=(300,20))
ping_sizer.Add(self.Ping_IP,0,wx.EXPAND)
self.Ping_Button=wx.Button(self,-1,u"PING测试")
self.Bind(wx.EVT_BUTTON,self.OnClicK_PING,self.Ping_Button)
ping_sizer.Add(self.Ping_Button,0,wx.ALIGN_BOTTOM|wx.ALIGN_RIGHT)
self._message_info=wx.StaticText(self,-1,u'',(20,20))
about_midSizer.Add(CreateCenterSizer(ping_sizer, 10), 1, wx.EXPAND)
about_mainSizer.Add(self.cpu_midSizer,0,wx.EXPAND)
about_mainSizer.Add(sizer,0,wx.EXPAND)
about_mainSizer.Add(self._part_label,0,wx.EXPAND)
about_mainSizer.Add(self._part_stat,0,wx.EXPAND)
about_mainSizer.Add(self._disk_label,0,wx.EXPAND)
about_mainSizer.Add(self._disk_stat,0,wx.EXPAND)
about_mainSizer.Add(about_midSizer,0,wx.EXPAND)
about_mainSizer.Add(self._message_info,0,wx.EXPAND)
self.SetSizer(CreateCenterSizer(about_mainSizer, 10))
self.OnStart(None)
def OnStart(self, event ):
self.timer.Start(1000)
def OnTimer(self, evt):#显示时间事件处理函数
cpu_stat=os.popen("mpstat |tail -n 1|awk '{print $4,$6,$7,$NF}'").read()
mem_stat=os.popen("top -bi -n 1|grep -w Mem|head -n 1").read()
part_stat=os.popen("df -h |grep -v tmpfs|awk '{print $1,$2,$3,$4,$5,$6}'").read()
cpu_air=os.popen("sensors|awk '/Physical id 0/{print $4}'|sed 's/+//'").read()
disk_stat=os.popen("iostat -x |grep -A20 Device |awk '{print $1,$2,$3,$10,$NF}'").read()
currt_cpu_ghz=os.popen("""cpupower frequency-info|grep "current CPU"|awk '{print $5$6}'|head -n 1""").read().strip("\n")
self._cpu_stat.SetLabel(cpu_stat)
self.cpu_curt_GHZ.SetLabel(currt_cpu_ghz)
self._cpu_air_stat.SetLabel(cpu_air)
self._mem_stat.SetLabel(mem_stat)
self._part_stat.SetLabel(part_stat)
self._disk_stat.SetLabel(disk_stat)
def OnClicK_PING(self,event):
PING_IP=self.Ping_IP.GetValue()
if (ipFormatChk(PING_IP)):
p_cmd="ping -c 4 -w 10 %s " % PING_IP
p=subprocess.Popen(p_cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
p_result=p.stdout.read()
p_regex=re.findall("100% packet loss",p_result)
if (len(p_regex)==0):
self._message_info.SetLabel(u"本机到达%s质量良好" % PING_IP)
else:
self._message_info.SetLabel(u"本机到达%s网络不可达" % PING_IP)
else:
self._message_info.SetLabel(u"请输入正确的IP地址")
def OnChar(self, event):
pass
def OnSave(self):
pass
class TAEMPanel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self, parent)
self._vm=MGRVM()
self._vm_hbox=wx.BoxSizer(wx.VERTICAL)
self._vm_sizer=wx.BoxSizer(wx.HORIZONTAL)
self._vm_button2=wx.Button(self,10,u'关机')
self._vm_button=wx.Button(self,20,u'开机')
self.Bind(wx.EVT_BUTTON,self.vmDestroy,self._vm_button2)
self.Bind(wx.EVT_BUTTON,self.vmStart,self._vm_button)
self._vm_sizer.Add(self._vm_button,0)
self._vm_sizer.Add(self._vm_button2,0)
self._vm_hbox.Add(self._vm_sizer,0,wx.EXPAND)
self.SetSizer(CreateCenterSizer(self._vm_hbox,10))
def vmStart(self,event):
self._vm.dom0.create()
def vmDestroy(self,event):
self._vm.dom0.destroy()
def vmReboot(self,event):
self._vm.dom0.reboot()
class MGRVM():
def __init__(self):
try:
#conn=libvirt.open('qemu+ssh://192.168.1.249/system')
conn=libvirt.open('qemu:///system')
self.dom0=conn.lookupByName('TAE-Manager')
self.flags=0
except:
self.flags=1
def ipFormatChk(ips):
pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
if re.match(pattern, ips):
return True
else:
return False
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15]))[20:24])
def Modify_Do(Ip,Taem,Gateway,Dns,Mask):
CMD1='''sed -i "s/^IPADDR.*$/IPADDR=%s/g" /etc/sysconfig/network-scripts/ifcfg-br0''' % Ip
CMD2='''sed -i "s/^GATEWAY.*$/GATEWAY=%s/g" /etc/sysconfig/network-scripts/ifcfg-br0''' % Gateway
CMD3='''sed -i "s/^NETMASK.*$/NETMASK=%s/g" /etc/sysconfig/network-scripts/ifcfg-br0''' % Mask
CMD4='''sed -i "s/^Server=.*$/Server=127.0.0.1,%s/g" /etc/zabbix/zabbix_agentd.conf''' % Taem
CMD5='''sed -i "s/^ServerActive=.*$/ServerActive=127.0.0.1,%s/g" /etc/zabbix/zabbix_agentd.conf''' % Taem
CMD6='''sed -i "s/^SERVER_IP=.*$/SERVER_IP=%s/g" /home/teraee/.bash_profile'''% Taem
CMD7='''echo nameserver %s > /etc/resolv.conf''' % Dns
CMD8="systemctl stop NetworkManager;ifdown br0; ifup br0"
print CMD1
print CMD2
print CMD3
print CMD4
print CMD5
print CMD6
print CMD7
'''
os.system(CMD1)
os.system(CMD2)
os.system(CMD3)
os.system(CMD4)
os.system(CMD5)
os.system(CMD6)
os.system(CMD7)
os.system(CMD8)
'''
class MYAPP(wx.App):
def OnInit(self):
frame=SettingDialog(None)
frame.Show(True)
return True
def main():
app=MYAPP()
app.MainLoop()
#app.MainLoop()
if __name__=="__main__":
main()
其中用到了 定时器: 动态显示内存cpu信息
浙公网安备 33010602011771号