繁体   English   中英

如何在 Python 的后台运行反向 ssh [重复]

[英]How to run reverse ssh in backgroud in Python [duplicate]

目标是建立从machine-a到machine-b的反向ssh连接,这样我就可以在建立反向连接后从machine-b ssh连接到machine-a。

导入os模块后,我继续使用os.system使用ssh -R 2210:localhost:22 me@80.54.0.107 &命令 ssh 连接到远程服务器。 请注意,命令行以&字符结尾,因为我想将 ssh 进程置于后台:

import os 
result = os.system("ssh -R 2210:localhost:22 me@80.54.0.107 &")
print("...started ssh connection", result)

但是我收到错误,结果没有建立 ssh 连接:

Pseudo-terminal will not be allocated because stdin is not a terminal.

我试图用subprocess运行它,希望它能工作:

import subprocess
cmd = ["ssh", "-R", "2210:localhost:22", "me@80.54.0.107", "&"]
result = subprocess.Popen(cmd, shell=False)

但它引发了另一个错误:

bash: -c: line 0: syntax error near unexpected token `&'

有没有办法从 Python 运行 ssh 连接并将其置于后台?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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