我编写了一个Python脚本,以使用密钥身份验证连接到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)
答案 0 :(得分:1)
我相信这是pysftp中的错误。每次使用cnopts.hostkeys = None
都会得到此提示(尽管警告实际上建议您使用该提示)。
无论如何,您不应该使用cnopts.hostkeys = None
,因为这样做会失去安全性。
有关正确的解决方案,请参见:
Verify host key with pysftp
尽管要避免出现警告,但必须将主机密钥存储到标准位置(如警告消息中所示,C:\Users\Rohan\.ssh\known_hosts
)。
通过参考密钥身份验证,我假设您将您的帐户密钥与主机密钥相混淆。 阅读我关于SSH key pairs的文章,以了解两者之间的区别。
答案 1 :(得分:1)
这是最新的 pysftp 中的一个错误,即使您设置了 CnOpts.hostkeys = None,仅实例化 CnOpts() 的行为就会使 pysftp 查找 known_hosts 文件,然后在未找到时发出警告。所以我只是进入代码并注释掉警告并扔了一些通行证。我别无选择,因为警告消息导致下游出错。关键是您可以在这里实施您自己的聪明解决方案:
##C:\Python38\Lib\site-packages\pysftp\__init__.py
class CnOpts(object): # pylint:disable=r0903
def __init__(self, knownhosts=None):
self.log = False
self.compression = False
self.ciphers = None
if knownhosts is None:
knownhosts = known_hosts()
self.hostkeys = paramiko.hostkeys.HostKeys()
try:
self.hostkeys.load(knownhosts)
except IOError:
# can't find known_hosts in the standard place
# wmsg = "Failed to load HostKeys from %s. " % knownhosts
# wmsg += "You will need to explicitly load HostKeys "
# wmsg += "(cnopts.hostkeys.load(filename)) or disable"
# wmsg += "HostKey checking (cnopts.hostkeys = None)."
# warnings.warn(wmsg, UserWarning)
pass
else:
pass
# if len(self.hostkeys.items()) == 0:
# raise HostKeysException('No Host Keys Found')