这是我非常简单的代码段,旨在通过端口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/