不退出 ssh_tunnel,打开新终端 [英] Without exiting from the ssh_tunnel, open new terminal

查看:56
本文介绍了不退出 ssh_tunnel,打开新终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Python 和 wxpython 作为 gui.我正在尝试连接 ssh 隧道.连接到 ssh 后,想要打开一个新终端,并且必须在本地机器上继续我的操作.如何实现这一目标?

我尝试了 subprocess、pexpect 和 paramiko,但都能够连接到 ssh 但不能打开新的终端

在我的代码下面是我用 pexpect 尝试过的:

导入时间导入系统进口预期c = pexpect.spawn("ssh -Y -L xxxx:localhost:xxxx 用户@host.com")时间.睡眠(0.1)c.expect("[pP]aasword")c.sendline("xxxxxx")时间.睡眠(0.2)c.interact()c.pexpect([user@host.com~]$)# 这里在它连接到 ssh 之后命令不会被执行c.sendline("xfce4-终端")

2013 年 4 月 24 日我能够打开新终端,但是当新终端从 gui 中打开控件时会发生什么,但不会去那里.有什么帮助吗?

解决方案

打开一个新的本地终端并将现有进程连接到其中有点复杂.至少有三种方法:

  1. 在开始连接之前打开终端,并运行所有尝试从其中建立连接的代码.这是最简单的.主要缺点是即使连接失败也会出现终端,这可能是您想要避免的.

  2. 使用 tmuxscreen 会话运行连接尝试,如果您检测到它成功,则将该会话重新连接到新终端.

  3. 让您的 Python 程序提供终端可以使用的 pty附加到 - 您的程序需要在远程连接和 pty 之间传递输入和输出.

I am using Python and wxpython for gui. I am trying to connect ssh tunnel. After connecting to ssh, wants a new terminal to open and have to continue my operation in local machine. How to achieve this?

I tried subprocess, pexpect and paramiko, but all are capable to connect to ssh but not open the new teminal

Below my code is there which I tried with pexpect:

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~]$)
# here after its connects to ssh then command wont be executed
c.sendline("xfce4-terminal")

On 24/04/2013 I am able to open new terminal but what happens is when the new terminal will open controls from gui doesn't go there. Any help?

解决方案

Opening a new local terminal and connecting an existing process in to it is a little complicated. There are at least three approaches:

  1. Open the terminal before you start connecting, and run all the code that tries to establish the connection from within it. This is simplest. The main drawback is that the terminal will appear even if the connection fails, which might be what you want to avoid.

  2. Run the connection attempt with a session of tmux or screen and if you detect that it succeeded then reattach that session in to a new terminal.

  3. Make your Python program provide a pty that the terminal can attach to - your program will need to hang around and pass input and output between the remote connection and the pty.

这篇关于不退出 ssh_tunnel,打开新终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆