利用paramiko,wxpython 实现一个连接linux,操作shell命令的功能,刚起步,后面有待发展,完善。
#coding:utf-8
import wximport paramiko
import threading
import platform
class SSHMainWindow(wx.App):
def OnInit(self):
self.width = 1000
self.hight = 480
frame = wx.Frame(parent=None, title='SHHWindow', size=(1000, 480))
self.panel = wx.Panel(frame, -1)
self.text = wx.TextCtrl(self.panel, -1,"", style=wx.TE_RICH|wx.TE_MULTILINE)
# menuBar = wx.MenuBar()
# self.menu = wx.Menu()
# login = self.menu.Append(-1, 'Login')
# menuBar.Append(self.menu, '&File')
# frame.SetMenuBar(menuBar)
self.startBtn = wx.Button(self.panel, -1,u"执行命令")
self.Bind(wx.EVT_BUTTON, self.OnStartButton, self.startBtn)
self.username=wx.StaticText(self.panel,-1,' User Name ')
self.usernametext = wx.TextCtrl(self.panel,-1,size=(100,-1))
self.password=wx.StaticText(self.panel,-1,' Password ')
self.passwordtext = wx.TextCtrl(self.panel,-1,size=(100,-1),style=wx.TE_PASSWORD)
self.IPadress=wx.StaticText(self.panel,-1,' IP ')
self.IP_adress=wx.TextCtrl(self.panel,-1,size=(100,-1))
self.shell=wx.StaticText(self.panel,-1,' SHELL ')
self.command = wx.TextCtrl(self.panel,-1,size=(200,-1),style=wx.TE_PROCESS_ENTER)
self.Bind(wx.EVT_TEXT_ENTER ,self.OnStartButton,self.command)
inner = wx.BoxSizer(wx.HORIZONTAL)
inner.Add(self.startBtn, 0, wx.RIGHT, 15)
inner.Add(self.username,0,wx.ALIGN_CENTER_VERTICAL)
inner.Add(self.usernametext,0,wx.ALIGN_CENTER_VERTICAL)
inner.Add(self.password,0,wx.ALIGN_CENTER_VERTICAL)
inner.Add(self.passwordtext,0,wx.ALIGN_CENTER_VERTICAL)
inner.Add(self.IPadress,0,wx.ALIGN_CENTER_VERTICAL)
inner.Add(self.IP_adress, 0, wx.ALIGN_CENTER_VERTICAL)
inner.Add(self.shell,0,wx.ALIGN_CENTER_VERTICAL)
inner.Add(self.command, 0, wx.ALIGN_CENTER_VERTICAL)
main = wx.BoxSizer(wx.VERTICAL)
main.Add(inner, 0, wx.ALL, 5)
main.Add(self.text, 1, wx.EXPAND|wx.ALL, 5)
self.panel.SetSizer(main)
frame.Show()
return True
def OnStartButton(self,event):
try:
global ssh
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.IP_adress.GetValue(), 22, self.usernametext.GetValue(), self.passwordtext.GetValue(), timeout=5)
stdin, stdout, stderr = ssh.exec_command(self.command.GetValue())
strout=(stdout.read()+stderr.read()).decode('utf-8', 'ignore')
self.text.WriteText('\r\n')
self.text.WriteText(strout)
ssh.close()
except Exception, e:
print e
self.text.WriteText(u"执行失败!")
return None
app = SSHMainWindow()
app.MainLoop()