我想使用paramiko在python中使用代理命令。
代理命令:
sftp -o "ProxyCommand /usr/bin/nc --proxy proxy.somegateway.com:8080 %h %p"
但是当我直接使用它时,我能够连接sftp服务器。
但如果我想在Python脚本中使用此代理命令,我会遇到以下问题:
我的剧本:
>>> import paramiko
>>> cmd = '/usr/bin/ssh proxy.somegateway.com:8080'
>>> proxy = paramiko.ProxyCommand(cmd)
>>> proxy
<paramiko.proxy.ProxyCommand object at 0x23b3bd0>
>>> client = paramiko.SSHClient()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(hostname='some.host.com', username='myuser', password='secretpassword', sock=proxy)
No handlers could be found for logger "paramiko.transport"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 366, in connect
t.start_client(timeout=timeout)
File "/usr/lib/python2.7/site-packages/paramiko/transport.py", line 510, in start_client
raise e
paramiko.ssh_exception.ProxyCommandFailure: ('/usr/bin/ssh proxy.somegateway.com:8080', 'Broken pipe')
我收到ProxyCommandFailure
问题。
答案 0 :(得分:1)
应该是这样的:
target_host = 'sftp.foo.com'
target_port = 22
proxy = paramiko.proxy.ProxyCommand(
'/usr/bin/nc --proxy proxy.bar.com:8080 %s %d' \
% (target_host, target_port) )
client.connect(hostname=target_host, port=target_port, password='...', sock=proxy)
# ^^^^^^^^^^
有关详细信息,请参阅paramiko的doc。
答案 1 :(得分:0)
请不要忘记将hostname
和port
添加到代理命令
target_host = 'foo'
target_port = 22
proxy = paramiko.proxy.ProxyCommand('/usr/bin/nc --proxy proxy.bar.com:8080 %s %d' % (target_hostname, target_port) )
更多信息here