用python的控件wx实现了windows通过ssh登录linux(学习一)

#!/usr/local/bin/python2.7
# encoding: gbk

import wx
import wx.grid
import os
import cPickle
import wx.lib.masked as masked
import subprocess
import paramiko
import time

class Example(wx.Frame):
    def __init__(self,parent,title):
        super(Example,self).__init__(parent,title=title,size=(500,480),style=wx.DEFAULT_FRAME_STYLE ^(wx.MAXIMIZE_BOX | wx.RESIZE_BORDER))
        self.InitUI()
        self.Centre()
        self.Show()
    def InitUI(self):
       
        menuBar = wx.MenuBar()
        filemenu = wx.Menu()
        helpmenu = wx.Menu()
        menuBar.Append(filemenu,"&文件")
        fitem = filemenu.Append(1000,"&新增IP","新能IP")
        self.Bind(wx.EVT_MENU, self.OnNew, id=1000)
        fitem = filemenu.Append(1001,"&导入IP","导入IP")
        self.Bind(wx.EVT_MENU, self.OnIn, id=1001) 
        fitem = filemenu.Append(1006,"&导出IP","导出IP")
        self.Bind(wx.EVT_MENU, self.OnOut, id=1006)       
        fitem = filemenu.Append(1002,"&退出系统","退出操作")
        self.Bind(wx.EVT_MENU, self.OnExit, id=1002)
        menuBar.Append(helpmenu,"&帮助")
        helpem = helpmenu.Append(1003,"&关于","关于")
        self.Bind(wx.EVT_MENU, self.OnAbout, id=1003)
        helpem = helpmenu.Append(1004,"&帮助","帮助")
        self.Bind(wx.EVT_MENU, self.OnHelp, id=1004)
        self.SetMenuBar(menuBar)
        self.SetTitle("演示环境启动程序 StartServerV1.0")
        self.SetBackgroundColour(wx.Colour(51,161,201))
        #######上面的代码主要是实现了窗体和菜单的建立
        panel=wx.Panel(self,-1)
        #text=wx.StaticText(panel,-1,"启动机器的IP地址:",(25,50))
        text=basicLabel = wx.StaticText(panel, -1, "启动IP地址:",(15,25))
        font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD)
        text.SetFont(font)
        self.basicText1 = wx.TextCtrl(panel,-1,size=(200, 270),pos=(110,10),style=wx.TE_MULTILINE)
       
        text=basicLabel = wx.StaticText(panel, -1, "Linux命令:",(19,315))
        font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD)
        text.SetFont(font)
       
        self.basicText2 = wx.TextCtrl(panel, -1, "请输入操作命令",
                size=(200, 30),pos=(110,310))
        self.Center()
        self.Show(True)
        #######上面的代码主要是实现了窗体文本框输入
        self.button1 = wx.Button(panel, -1, "全部启动", pos=(350, 50))
        self.button1.Bind(wx.EVT_BUTTON, self.OnOpen)
        #self.button = wx.Button(panel, -1, "单个启动", pos=(350, 70))
        self.button2 = wx.Button(panel, -1, "单机ping", pos=(350, 150))
        self.button2.Bind(wx.EVT_BUTTON, self.OnPing)
        #self.button = wx.Button(panel, -1, "修改IP", pos=(350, 170))
        #buttonDel = wx.Button(panel, -1, "单个删除", pos=(350, 220))
        self.button3 = wx.Button(panel, -1, "全部清空", pos=(350, 230))
        self.button3.Bind(wx.EVT_BUTTON, self.OnClear)
       
        #self.Bind(wx.EVT_BUTTON, self.Del, buttonDel)
        ##上面的代码主要是实现了按钮的布局
       
       
    def ReadFile(self):
        if self.filename:
            try:
                f = open(self.filename, 'r')
                data = cPickle.load(f)
                f.close()
                self.sketch.SetLinesData(data)
            except cPickle.UnpicklingError:
                wx.MessageBox("%s is not a sketch file." % self.filename,
                             "oops!", style=wx.OK|wx.ICON_EXCLAMATION)
    wildcard = "Sketch files (*.sketch)|*.sketch|All files (*.*)|*.*"
    def OnNew(self, event):
       dlg3=wx.SingleChoiceDialog(None,u"请选择下面IP地址:", u"IP地址",
                                   ['192.168.2.50','192.168.2.55','192.168.2.56','192.168.2.51','192.168.1.46','192.168.2.44',
                                    '192.168.2.45','192.168.2.47','192.168.2.48','192.168.2.49','192.168.2.52','192.168.2.53',
                                    '192.168.2.54'])
       if dlg3.ShowModal() == wx.ID_OK:
            self.response= dlg3.GetStringSelection()
            if self.basicText1.GetValue()=="":
                self.basicText1.AppendText(self.response)
            else:
                self.basicText1.AppendText("\n"+self.response)

       dlg3.Destroy()

        ###新增IP调用事件
    def OnIn(self, event):
        dlg = wx.FileDialog(self, "请选择要导入的IP文件...", os.getcwd(),
                           style=wx.OPEN, wildcard=self.wildcard)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetPath()
            self.ReadFile()
            self.SetTitle(self.title + ' -- ' + self.filename)
        dlg.Destroy()
        ###导入IP调用事件
    def OnSave(self, event):
        if not self.filename:
            self.OnSaveAs(event) 
        else:
            self.SaveFile()
    def OnOut(self, event):
        dlg = wx.FileDialog(self, "请选择要导 出的IP文件路径...", os.getcwd(),
                           style=wx.SAVE | wx.OVERWRITE_PROMPT,
                           wildcard = self.wildcard)
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()
            if not os.path.splitext(filename)[1]:
                filename = filename + '.sketch'
            self.filename = filename
            self.SaveFile()
            self.SetTitle(self.title + ' -- ' + self.filename)
        dlg.Destroy()
           ###导出IP调用事件
    def OnExit(self, event):       
        self.Close(True)
    def OnAbout(self, event):
        wx.MessageBox("Copyright(c)2013-2088 The Gooagoo\nAll Rights Reserved\nVersion 1.0。", u"关于",wx.ICON_INFORMATION)
    def OnHelp(self, event):
        wx.MessageBox("1、该系统适合远程启动linux命令。\n2、支持单个或多个机器操作。", u"帮助",wx.ICON_QUESTION)
        ###菜单事件响应   
    def OnPing(self,event):
        #self.button = wx.TextEntryDialog(None, u"请在下面IP地址:", u"IP地址输入框",u"例  192.168.2.168")
        dlg = wx.TextEntryDialog(None, "",'IP ping功能', '192.168.2.50')
        if dlg.ShowModal() == wx.ID_OK:
            ss = dlg.GetValue()
            p = subprocess.Popen('cmd.exe',shell=True,stdin=subprocess.PIPE)
            #wx.MessageBox("请输入要ping的IP")
            p.stdin.write('ping '+ss+' -t'+'\n')
            p.stdin.close()
       
        dlg.Destroy()
       
   
    def OnClear(self,event):
        self.basicText1.SetValue("")   
    def OnOpen(self,event):
        ssh = paramiko.SSHClient()
        cmd = self.basicText2.GetValue()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.50",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
        #第1台192.168.2.50
       
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.55",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
         #第2台192.168.2.55
         
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.56",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
         #第3台192.168.2.56
         
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.51",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
         #第4台192.168.2.51
         
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.46",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
         #第5台192.168.2.46
         
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.44",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
         #第6台192.168.2.44
         
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.45",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
         #第7台192.168.2.45
         
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.47",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
         #第8台192.168.2.47
         
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.48",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
         #第9台192.168.2.48
         
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.49",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
         #第10台192.168.2.49
         
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.52",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
         #第11台192.168.2.52
         
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.53",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
         #第12台192.168.2.53
         
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect("192.168.2.54",22,"root", "123456789")
        stderr = ssh.exec_command(cmd)
        time.sleep(45)
        ssh.close()
        #第13台192.168.2.54
if __name__ == '__main__':
    app = wx.App()
    Example(None,title='Layout1')
    app.MainLoop()
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值