这些日子在公司测试机顶盒游戏,每次启动游戏的时候都要在windows黑色的dos窗口输命令,非常不方便,于是借着自己刚学的一点点python+wxPython的知识,写了这个telnet客户端,功能比较简单,根据需要可以再作修改。界面用wxGlade生成,本来我不喜欢wxGlade的,觉得不是很好用,但用着用着发现其实还不错,生成的代码也很容易和自己的代码结合。
输入指令的输入框有默认的指令提示,为了方便写了个COMMAND指令列表,这是公司里用的,可以在源代码中COMMADN列表中修改自己需要的指令,或者去掉COMMAND,直接往输入框输指令。
#!/usr/bin/env python
# -*- coding: GB2312 -*-
# @author 小星星 [email protected]
# generated by wxGlade 0.6.3 on Mon Aug 17 13:50:13 2009
import wx
import telnetlib
import sys
# begin wxGlade: extracode
# end wxGlade
# 预先定义的指令列表
COMMAND=['busybox mount -t nfs -o nolock,ro,mountvers=2 192.168.1.128:/iptv /mnt','cd mnt/czy/llz/soundddz','sh run.sh']
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
# 初始化组件,由wxGlade产生
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.panel_1 = wx.Panel(self, -1)
self.sizer_3_staticbox = wx.StaticBox(self.panel_1, -1, u"远程登录")
self.label_2 = wx.StaticText(self.panel_1, -1, u"地址:")
self.ip_ctrl = wx.TextCtrl(self.panel_1, -1, "192.168.18.11")
self.label_3 = wx.StaticText(self.panel_1, -1, u"端口:")
self.port_ctrl = wx.TextCtrl(self.panel_1, -1, "23")
self.label_4 = wx.StaticText(self.panel_1, -1, u"用户:")
self.user_ctrl = wx.TextCtrl(self.panel_1, -1, "root")
self.label_5 = wx.StaticText(self.panel_1, -1, u"密码:")
self.pwd_ctrl = wx.TextCtrl(self.panel_1, -1, "6e58f908")
self.label_6 = wx.StaticText(self.panel_1, -1, u"连接信息")
self.info_ctrl = wx.TextCtrl(self.panel_1, -1, "", style=wx.TE_MULTILINE)
self.label_7 = wx.StaticText(self.panel_1, -1, u"发送指令")
self.cmd_ctrl = wx.TextCtrl(self.panel_1, -1, "", style=wx.TE_MULTILINE)
self.send_btn = wx.Button(self.panel_1, -1, u"发送指令")
self.static_line_1 = wx.StaticLine(self.panel_1, -1)
self.logout_btn = wx.Button(self.panel_1, -1, u"退出登录")
self.login_btn = wx.Button(self.panel_1, -1, u"远程登录")
self.clear_btn = wx.Button(self.panel_1, -1, u"清空信息")
self.exit_btn = wx.Button(self.panel_1, -1, u"退出程序")
# 组件添加属性值,由wxGlade产生
self.__set_properties()
self.__do_layout()
# 按钮事件绑定,由wxGlade产生
self.Bind(wx.EVT_BUTTON, self.OnSend, self.send_btn)
self.Bind(wx.EVT_BUTTON, self.OnLogout, self.logout_btn)
self.Bind(wx.EVT_BUTTON, self.OnLogin, self.login_btn)
self.Bind(wx.EVT_BUTTON, self.OnExit, self.exit_btn)
self.Bind(wx.EVT_BUTTON, self.OnClear, self.clear_btn)
# telnet连接标志
self.isconnected=False
# end wxGlade
# 连接远程计算机
def connect(self):
# 计数器
self.temp=0
# telnet登陆必须参数,端口默认23,这里就没再设置
self.HOST=str(self.ip_ctrl.GetValue())
self.USER=str(self.user_ctrl.GetValue())
self.PASS=str(self.pwd_ctrl.GetValue())
self.cmd_ctrl.SetValue(COMMAND[self.temp])
# 实例化一个telnet对象
self.tn=telnetlib.Telnet()
try:
# 连接,出现异常则宣告连接失败
self.tn.open(self.HOST)
self.info_ctrl.AppendText("已连上远程计算机,等待登录...\n")
return True
except:
self.info_ctrl.AppendText("对不起,远程计算机连接失败!\n")
return False
def login(self):
# 从远程计算机获取登录提示信息,需要输入用户名(包含"login"字符串)
user_info=self.tn.expect(['login:'],5)
self.info_ctrl.AppendText("反馈:"+user_info[2]+"\n")
# 发送用户名
self.tn.write(self.USER+'\n')
self.info_ctrl.AppendText("登录用户:"+self.USER+"\n")
# 从远程计算机获取登录提示信息,需要输入密码(包含"Password"字符串)
pwd_info=self.tn.expect(['Password:'],5)
self.info_ctrl.AppendText("反馈:"+pwd_info[2]+"\n")
# 发送密码
self.tn.write(self.PASS+'\n')
self.info_ctrl.AppendText("登录密码:"+self.PASS+"\n")
# 如果登陆成功,会回显用户名,这里根据是否回显用户名判断登陆是否成功
result=self.tn.expect([self.USER],5)
self.info_ctrl.AppendText("反馈:"+result[2]+"\n")
if result[0]==-1:
self.info_ctrl.AppendText("登录失败,请确认用户名和密码!\n")
return False
else:
self.info_ctrl.AppendText("恭喜你!登陆成功!\n")
self.info_ctrl.AppendText("等待输入指令......\n")
#为了减少用户错误操作,这里加了些手段
self.logout_btn.Enable(True)
self.login_btn.Enable(False)
self.send_btn.Enable(True)
self.isconnected=True
return True
# 发送telnet指令
def sendCommand(self,cmd):
self.send_btn.Enable(False)
self.info_ctrl.AppendText("指令:"+cmd+"\n")
self.tn.write(cmd+'\n')
reply=self.tn.expect(['%'],5)
self.info_ctrl.AppendText("反馈:"+reply[2]+"\n")
self.info_ctrl.AppendText("等待输入指令......\n")
self.send_btn.Enable(True)
# 退出telnet登陆
def logout(self):
self.tn.close()
self.info_ctrl.AppendText("成功退出登录!\n")
del self.tn
self.send_btn.Enable(False)
self.logout_btn.Enable(False)
self.login_btn.Enable(True)
self.isconnected=False
# 组件属性赋值,由wxGlade产生
def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("小星星远程登录工具")
self.SetSize((618, 463))
self.info_ctrl.SetMinSize((600, 180))
self.cmd_ctrl.SetMinSize((600, 50))
self.send_btn.Enable(False)
self.logout_btn.Enable(False)
# end wxGlade
# 界面布局,由wxGlade产生(wxGlade的确是个好东西!)
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_2 = wx.BoxSizer(wx.VERTICAL)
sizer_3 = wx.StaticBoxSizer(self.sizer_3_staticbox, wx.VERTICAL)
grid_sizer_4 = wx.GridSizer(1, 4, 0, 0)
grid_sizer_3 = wx.GridSizer(1, 1, 0, 0)
sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
sizer_8 = wx.BoxSizer(wx.HORIZONTAL)
sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
sizer_5.Add(self.label_2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_5.Add(self.ip_ctrl, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_4.Add(sizer_5, 1, wx.EXPAND, 0)
sizer_6.Add(self.label_3, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_6.Add(self.port_ctrl, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_4.Add(sizer_6, 1, wx.EXPAND, 0)
sizer_7.Add(self.label_4, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_7.Add(self.user_ctrl, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_4.Add(sizer_7, 1, wx.EXPAND, 0)
sizer_8.Add(self.label_5, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_8.Add(self.pwd_ctrl, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_4.Add(sizer_8, 1, wx.EXPAND, 0)
sizer_3.Add(sizer_4, 1, wx.EXPAND, 0)
sizer_3.Add(self.label_6, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_3.Add(self.info_ctrl, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_3.Add(self.label_7, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_3.Add(self.cmd_ctrl, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
grid_sizer_3.Add(self.send_btn, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_3.Add(grid_sizer_3, 1, wx.EXPAND, 0)
sizer_3.Add(self.static_line_1, 0, wx.EXPAND, 0)
grid_sizer_4.Add(self.exit_btn, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
grid_sizer_4.Add(self.clear_btn, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
grid_sizer_4.Add(self.logout_btn, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
grid_sizer_4.Add(self.login_btn, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
sizer_3.Add(grid_sizer_4, 1, wx.EXPAND, 0)
self.panel_1.SetSizer(sizer_3)
sizer_2.Add(self.panel_1, 1, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
self.SetSizer(sizer_2)
self.Layout()
# end wxGlade
# 响应"发送指令"按钮动作,执行发送操作
def OnSend(self, event): # wxGlade: MyFrame.<event_handler>
self.sendCommand(str(self.cmd_ctrl.GetValue()))
self.temp=self.temp+1
if self.temp>=len(COMMAND):
self.cmd_ctrl.SetValue("")
else:
self.cmd_ctrl.SetValue(COMMAND[self.temp])
# 响应"退出登录"按钮动作,执行退出登录操作
def OnLogout(self, event): # wxGlade: MyFrame.<event_handler>
self.logout()
# 响应"远程登录"按钮动作,执行登录操作
def OnLogin(self, event): # wxGlade: MyFrame.<event_handler>
self.connect()
self.login()
self.temp=0
# 响应"退出程序"按钮动作,执行退出程序操作
def OnExit(self,event):
# 当然,如果登陆的时候强制退出,为了安全,弹出提示
if self.isconnected:
dlg=wx.MessageDialog(parent=None,message="如果现在退出,将会丢失远程登录连接,是否确认退出?",caption="警告",style=wx.OK|wx.CANCEL)
if dlg.ShowModal()==wx.ID_OK:
self.logout()
self.Close()
sys.exit()
else:
self.Close()
sys.exit()
# 响应"清空信息"按钮动作,执行清空消息内容
def OnClear(self,event):
self.info_ctrl.SetValue("")
# end of class MyFrame
# 主程序入口,这个也是wxGlade产生的
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 =MyFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()