gpt4 book ai didi

python - 使用 pysftp 连接到 SFTP 服务器时出现 "Failed to load HostKeys"警告

转载 作者:太空宇宙 更新时间:2023-11-04 04:09:49 32 4
gpt4 key购买 nike

我编写了一个 Python 脚本来使用 key 身份验证连接到 SFTP 服务器。它成功连接到服务器但显示以下警告(见下文)。这是什么意思以及如何删除它。必须对代码进行哪些更改?

我的代码:

import os
import pysftp
import socket
import paramiko
import time
import os.path
import shutil

IP = "127.0.X.X"
myUsername = "USERNAME"
port = 22

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

import os
privatekeyfile = os.path.expanduser("C:\\Users\\Rohan\\.ssh\\cool.prv")
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)

try:
with pysftp.Connection(host=IP, username=myUsername,private_key=mykey,cnopts=cnopts) as sftp:
try:
r=str(socket.gethostbyaddr(IP))
print("connection successful with "+r)

except socket.herror:
print("Unknown host")
except:
print("connection failed")

警告:

UserWarning: Failed to load HostKeys from C:\Users\Rohan\.ssh\known_hosts.  You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
warnings.warn(wmsg, UserWarning)

最佳答案

我相信这是 pysftp 中的错误。每次使用 cnopts.hostkeys = None 时都会得到这个(尽管警告实际上建议使用它)。

无论如何,你不应该使用 cnopts.hostkeys = None,这样做会失去安全性。
有关正确的解决方案,请参阅 Verify host key with pysftp .


根据您提到的 key 身份验证,我假设您将帐户 key 误认为是主机 key 。阅读我关于 SSH key pairs 的文章了解差异。

关于python - 使用 pysftp 连接到 SFTP 服务器时出现 "Failed to load HostKeys"警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56521549/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com