如何在wxpython中与ssh交互?

时间:2018-08-12 12:40:42

标签: python-3.x subprocess wxpython io-redirection wxpython-phoenix

环境

  • ubuntu 16.04
  • python 3.6.6
  • wxpython 4.0.3

我想做什么

我想在wxpython中使用ssh。下面的gui显示。我想在textctrl中显示ssh输出,并在textctrl中输入命令。 it is the gui snapshot

我做了什么

我认为我应该将ssh的输出重定向到up textctrl,并将down textctrl中的cmd文本发送到ssh。我写的代码很烂,ShellFrameBase是wxFormBuilder生成的gui类。

class ShellFrame(ShellFrameBase):
    def __init__(self, parent):
        ShellFrameBase.__init__(self, parent)
        t = threading.Thread(target=self.run_shell)
        t.start()

    def run_shell(self):
        args = "ssh -v user@remote"
        # args = "ping 8.8.8.8"
        self.shell = subprocess.Popen(args.split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        for line in self.shell.stdout:
            wx.CallAfter(self.shell_text.AppendText, line)

    def send_cmd(self, event):
        cmd = self.cmd_text.GetValue()
        self.shell.stdin.write(cmd + '\n')
        self.shell.stdin.flush()

if __name__ == '__main__':
    app = wx.App(False)
    frame = ShellFrame(None)
    frame.Show(True)
    app.MainLoop()

发生了什么

  1. 当使用“ ping 8.8.8.8”作为参数时,输出显示为this ping snapshot
  2. 当将“ ssh -v user @ remote”用作args时,输出不显示任何内容。
  3. 如果我使用“ ssh -v user @ remote”作为参数,并在subprocess.Popen中删除“ stdin = subprocess.PIPE”,则输出显示为this ssh snapshot
  4. 我不知道如何将cmd发送到子进程。

问题

我不知道该怎么办。有人会给我一个线索吗?或者我可以用其他方式做到这一点?

0 个答案:

没有答案