【问题标题】:"No such file or directory" when call ProxyCommand through Python script通过 Python 脚本调用 ProxyCommand 时“没有这样的文件或目录”
【发布时间】:2019-08-23 22:12:32
【问题描述】:

这是我非常简单的一段代码,旨在通过端口 8888 上的本地 socks 代理连接到 ssh 服务器

    import subprocess  
    host = 'X.x.X.x'
    port = 22
    subprocess.call(  [
      "ssh", 
      "-o", "ProxyCommand='/bin/socat - SOCKS4A:127.0.0.1:%h:%p,socksport=8888'",
      "-p", "{}".format(port),
      "root@{}".format(host)
    ])

但是,我在尝试执行时收到了一条丑陋的错误消息。

    /bin/bash: /bin/socat - SOCKS4A:127.0.0.1:X.x.X.x:22,socksport=8888: No such file or directory
    ssh_exchange_identification: Connection closed by remote host

trange 是直接复制粘贴整个命令行到 shell 上的工作。

【问题讨论】:

    标签: python ssh subprocess socat


    【解决方案1】:

    有点晚了,但我在使用 execvp 运行时遇到了同样的问题。您需要删除 ProxyCommand 周围的引号,例如

    import subprocess  
    host = 'X.x.X.x'
    port = 22
    subprocess.call(  [
      "ssh", 
      "-o", "ProxyCommand=/bin/socat - SOCKS4A:127.0.0.1:%h:%p,socksport=8888",
      "-p", "{}".format(port),
      "root@{}".format(host)
    ])
    

    当您在命令行上运行时,这些会被您的 shell 删除。

    【讨论】:

    • 谢谢!已经坚持了一段时间,但可以确认这是有效的。
    猜你喜欢
    • 2013-11-14
    • 2017-06-16
    • 2016-01-11
    • 2018-08-24
    • 2017-05-02
    • 2015-12-29
    • 2018-03-24
    相关资源
    最近更新 更多