[autossh在python中执行时不起作用

问题描述 投票:0回答:2

我正在尝试创建ssh反向隧道。当我在终端上运行以下命令时,效果很好:

autossh -M 10876 -N -f -o PubkeyAuthentication=yes -o PasswordAuthentication=no -i /root/.ssh/id_rsa -R 6666:localhost:22 root@**.**.**.** -p 2233

但是当我用os.system()subprocess在python中运行它时,它不起作用。当我检查进程列表时,该进程已创建并正在运行,并且与最后一个方法没有区别。(直接在终端中运行)

python代码:

command ='autossh -M 10876 -N -f -o PubkeyAuthentication=yes -o PasswordAuthentication=no -i /root/.ssh/id_rsa -R 6666:localhost:22 root@**.**.**.** -p 2233'
proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, shell=True)

有什么问题吗?

python linux terminal subprocess autossh
2个回答
2
投票
[当您使用shell=True时,您不想在命令参数上使用shlex.split() ...它应该只是一个字符串。

因为您将args作为序列传递给Popen,所以第一个arg(autossh)被用作命令,而其余的args实际上传递给

shell,而不是autossh ]。

所以...您可以删除shell=True,也可以通过删除使用shlex.split()将args作为字符串传递。

https://docs.python.org/2/library/subprocess.html#subprocess.Popen


0
投票
[它在Windows10中对我有效(cygwin中的autossh,Python 3.6)port = 6379; command = f'autossh -M 1{port} -N -f -C -L {port}:localhost:{port} root@acone2'; proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, shell=True)
© www.soinside.com 2019 - 2024. All rights reserved.