获取错误
paramiko.ssh_exception.SSHException: No hostkey for host target.org found.
当使用 pysftp
(对于需要特定端口的连接)时,即使我提供了最初用于连接到该位置的相同 known_hosts 文件(在帖子 here 之后) . IE。做了……
[airflow@airflowetl reporting]$ sftp -oPort=15259 my_user@target.org
The authenticity of host '[target.org]:15259 ([205.172.2.88]:15259)' can't be established.
RSA key fingerprint is SHA256:UM6OflG0rkcYohes7qOlYoJZ4TIqVd0JQSh7HXYZQVA.
RSA key fingerprint is MD5:33:c2:30:22:57:5b:57:98:2f:11:07:4d:a3:4a:10:0f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[target.org]:15259,[205.172.2.88]:15259' (RSA) to the list of known hosts.
password
Enter password for my_user
Password:
Connected to target.org.
sftp> ls
csv_drop test_results
sftp> exit
然后将/home/me/.ssh/known_hosts
复制到另一个位置
[airflow@airflowetl .ssh]$ ls -lha
total 8.0K
drwx------ 2 airflow airflows 25 Oct 19 17:31 .
drwx------. 32 airflow airflows 4.0K Oct 19 17:31 ..
-rw-r--r-- 1 airflow airflows 777 Oct 19 17:32 known_hosts
[airflow@airflowetl .ssh]$ ls -lha /home/airflow/projects/backups/reporting/configs/known_hosts
-rw-r--r-- 1 airflow airflows 777 Oct 19 17:34 /home/airflow/projects/backups/reporting/configs/known_hosts
(注意权限是原件和副本的名称)像...一样使用
# connect to ftp location
assert os.path.isfile(os.path.join(PROJECT_HOME, "configs", "known_hosts"))
cnopts = pysftp.CnOpts(knownhosts=os.path.join(PROJECT_HOME, "configs", "known_hosts"))
#cnopts = pysftp.CnOpts(knownhosts="/home/airflow/.ssh/known_hosts")
HOSTNAME = "target.org"
PORT = 15259
sftp = pysftp.Connection(HOSTNAME,
port=PORT,
username=CREDS["sink"]["ftp_creds"]["username"],
password=CREDS["sink"]["ftp_creds"]["password"],
cnopts=cnopts)
print(sftp.pwd())
print(sftp.listdir())
sftp.cwd(CONF["reporting_configs"]["to"]["share_folder_path"])
print(sftp.pwd())
print(sftp.listdir())
然而,当运行脚本时,我收到错误消息,提示找不到主机 key (请注意,即使使用原始的 known_hosts 文件路径也会引发相同的错误(无论使用主机名还是 IP 都会发生)。是什么原因造成的?我可以尝试获取更多信息的更多调试步骤吗?对这种事情没有太多经验。
在运行时查看调试器,我看到 cnopts hostkeys 条目似乎确实包含正确的主机 key ...
<HostKeyEntry ['[target.org]:15259', '[205.172.2.88]:15259']: <paramiko.rsakey.RSAKey object at 0x7f8d752d4208>>
请注意,主机 key 条目是'[target.org]:15259'(即,它结合了指定的端口),即使作为连接函数的服务名称提供的名称只是“target”。组织'
完整的回溯看起来像...
Traceback (most recent call last):
File "./source/local2ftp.2.py", line 52, in <module>
cnopts=cnopts)
File "/home/airflow/projects/backups/reporting/venv/lib/python3.6/site-packages/pysftp/__init__.py", line 132, in __init__
self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
File "/home/airflow/projects/backups/reporting/venv/lib/python3.6/site-packages/pysftp/__init__.py", line 71, in get_hostkey
raise SSHException("No hostkey for host %s found." % host)
paramiko.ssh_exception.SSHException: No hostkey for host target.org found.
Exception ignored in: <bound method Connection.__del__ of <pysftp.Connection object at 0x7f1a61df6f98>>
Traceback (most recent call last):
File "/home/airflow/projects/backups/reporting/venv/lib/python3.6/site-packages/pysftp/__init__.py", line 1013, in __del__
self.close()
File "/home/airflow/projects/backups/reporting/venv/lib/python3.6/site-packages/pysftp/__init__.py", line 784, in close
if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live'
请注意,我最初在命令行中通过 sftp
连接并在 pysftp
脚本中使用的用户不是运行脚本的用户(我的用户用于连接到 sftp 服务器的是一组特殊的连接凭据)。我不知道这是否相关。
最佳答案
pysftp 不支持带有端口的主机 key 条目。
- 要么跳过 pysftp,直接使用 Paramiko。
- 或者通过将
known_hosts
文件中的[target.org]:15259
替换为target.org
来破解它。
关于python - pysftp 抛出 paramiko.ssh_exception.SSHException 即使提供了 known_hosts 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64438184/