【问题标题】:autossh not working when executed in python在python中执行时autossh不起作用
【发布时间】:2016-03-19 00:52:21
【问题描述】:

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

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

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

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


    【解决方案1】:

    当您使用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

    【讨论】:

    • 我试过你的答案,但没有任何区别。还是无法建立 ssh 连接。
    【解决方案2】:

    它适用于 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)

    【讨论】:

      猜你喜欢
      • 2018-01-26
      • 2022-08-09
      • 2017-08-02
      • 1970-01-01
      • 2017-08-30
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      • 2018-07-10
      相关资源
      最近更新 更多