我正在尝试创建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)
有什么问题吗?
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
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)