简体   繁体   中英

Pop up message box after connecting to SSH

Hi all I am learning Python and making a GUI model using wxPython.

I want to connect vai SSH, I am used pexpect for this purpose. I want to display a msg box saying, "Connected to server" or if disconnected, "connection not established" I am cannot figure out how to do this and the GUI freezes when it connects. How do I avoid freezing the GUI? My sample code is:

import time
import sys
import pexpect
c = pexpect.spawn("ssh -Y -L xxxx:localhost:xxxx user @ host.com")
#time.sleep(0.1)
c.expect("[pP]aasword")
c.sendline("xxxxxx")
#time.sleep(0.2)
c.interact()
c.pexpect([user@host.com~]$)

After its connects to SSH here, the GUI freezes. After connecting, I want show the connection status in a message box, not in terminal. Please suggest how to do it; as a beginner I find it difficult.

Thanks in advance.

Update:

import wx
import os
import pexpect
import sys
import subprocess
import time
class Connect_ssh(wx.Frame):
    def __init__ (self, *args, **kw):
        wx.Frame.__init__(self,None,wx.ID_ANY,"Secure Shell",    size=(310,200),style=wx.DEFAULT_FRAME_STYLE ^ wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER)
        panel = wx.Panel(self)
        txt1 = wx.StaticText(panel, label="Account name:",pos=(20, 55))
        txt2 = wx.StaticText(panel, label="Password",pos=(20, 105))
        self.txt_name = wx.TextCtrl(panel, -1, size=(130, -1), pos=(160,50))
        self.txt_pswd= wx.TextCtrl(panel, -1, size=(130, -1),pos= (160,100),style=wx.TE_PASSWORD)
        button1 = wx.Button(panel, -1, "Connect",size=(-1,-1), pos=(50, 160))
        button2 = wx.Button(panel, -1, "Disconnect",size=(-1,-1), pos=(170, 160))
        self.Bind(wx.EVT_BUTTON,self.OnConc,button1)
   def OnConc(self,event):
        u_name = self.txt_name.GetValue()
        passwd = self.txt_pswd.GetValue()
        child = pexpect.spawn("ssh -Y -L xxx:localhost:xxx %s@host.com" % (str(u_name)))
        child.expect("%s@host.com's password:" % (str(u_name)) )
        child.sendline("%s" % (str(passwd)))
        child.interact()
        #child.sendline("%s" % str(sub))
        child.expect("[%s@qvislabs.com~]$"% (str(u_name)) )
        #time.sleep()
        #self.Destroy()
        msg = wx.MessageBOx(" '%s'@host.com is connected" % (str(u_name)), "Info",wx_OK)
        self.Hide()

if __name__=="__main__":
app = wx.App()
Connect_ssh().Show()
app.MainLoop()

The GUI is probably freezing because the SSH connection is blocking the main loop. To overcome this issue, you'll have to put the connecting code into a separate thread. Then use one of wxPython's thread-safe methods (wx.CallAfter, wx.CallLater or wx.PostEvent) to tell the GUI to display a pop-up dialog.

See the following links for information on wxPython and threads:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM