我正在使用以下SSH配置下载一些文件:
Host proxy
Hostname proxy.example.com
User proxyuser
IdentityFile ~/.ssh/proxy_id_rsa
Host target # uses password auth
Hostname target.example.com
User targetuser
ProxyCommand ssh proxy nc %h %p
我试图自动下载文件 - 目前正在使用paramiko,但如果更容易,可以使用其他库。
以下是我根据其他答案尝试的内容:
from paramiko.proxy import ProxyCommand
from paramiko.transport import Transport
from paramiko.sftp_client import SFTPClient
proxy = ProxyCommand('ssh -i /Users/ben/.ssh/proxy_id_rsa proxyuser@proxy.example.com nc target.example.com 22')
client = SFTPClient(proxy)
client.connect(username='targetuser', password='targetpassword')
然而,这会引发错误
Traceback (most recent call last):
File "sftp.py", line 6, in <module>
client = SFTPClient(proxy)
File "/Users/ben/.virtualenvs/venv/lib/python3.5/site-packages/paramiko/sftp_client.py", line 99, in __init__
server_version = self._send_version()
File "/Users/ben/.virtualenvs/venv/lib/python3.5/site-packages/paramiko/sftp.py", line 105, in _send_version
t, data = self._read_packet()
File "/Users/ben/.virtualenvs/venv/lib/python3.5/site-packages/paramiko/sftp.py", line 177, in _read_packet
raise SFTPError('Garbage packet received')
paramiko.sftp.SFTPError: Garbage packet received
不幸的是,错误消息不是很有帮助,所以我可能会因为我可以改变而感到茫然。我无法更改target
上的配置,如果可以避免,我不想更改proxy
上的配置。有什么建议吗?
答案 0 :(得分:0)
解决以下问题:
class PatchedProxyCommand(ProxyCommand):
# work around https://github.com/paramiko/paramiko/issues/789
@property
def closed(self):
return self.process.returncode is not None
@property
def _closed(self):
# Concession to Python 3 socket-like API
return self.closed
def close(self):
self.process.kill()
self.process.poll()
proxy = PatchedProxyCommand('ssh -i /Users/ben/.ssh/proxy_id_rsa '
'proxyuser@proxy.example.com nc target.example.com 22')
transport = Transport(proxy)
key = HostKeyEntry.from_line('target.example.com ssh-rsa '
'AAAAB3NzaC1yc2EAAAA/base64+stuff==').key
transport.connect(hostkey=key,
username='targetuser', password='targetpass')
sftp = SFTPClient.from_transport(transport)
print(sftp.listdir())