"Failed to load HostKeys" warning while connecting to SFTP server with pysftp
I believe it's a bug in pysftp. You get this everytime you use cnopts.hostkeys = None
(despite the warning actually suggesting to use that).
Anyway, you should not use cnopts.hostkeys = None
, you lose security by doing so.
For the correct solution, see Verify host key with pysftp.
By your reference to key authentication, I assume you mistake your account key with host key. Read my article about SSH key pairs to understand the difference.
It's a bug in the latest pysftp, even though you set CnOpts.hostkeys = None, just the act of instantiating CnOpts() makes pysftp look for the known_hosts file and then raise the warning if it's not found. So I just went in the code and commented out the warning and threw in some passes. I didn't have a choice because the warning messages were causing errors downstream. The point is you can implement your own clever solution here:
##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')