python - 通过python脚本调用ProxyCommand时“No such file or directory”

标签 python ssh subprocess socat

这是我非常简单的代码段,旨在通过端口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 上时起作用的。

最佳答案

有点晚了,但是使用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 程序删除。

关于python - 通过python脚本调用ProxyCommand时“No such file or directory”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55477244/

相关文章:

python - 共享模块变量没有更新?

python + urllib2 : streaming ends prematurely

github - github 访问 token 背后​​的原因是什么?

linux - centos6.5中的HostKey算法

image - 如何使用 2 个图像 ffmpeg 创建图像

python - 替换for循环Python中的一部分文本元素

python - 如果句子以标点符号结尾,则在不插入无效标记的情况下分割标点符号的有效方法

Ubuntu - ssh/ftp 用户的 chroot 虚拟系统

java - Python子进程不传递参数

linux - 子进程库不会执行 compgen