"failed To Load Hostkeys" Warning While Connecting To Sftp Server With Pysftp
Solution 1:
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.
Solution 2:
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__.pyclassCnOpts(object): # pylint:disable=r0903def__init__(self, knownhosts=None):
self.log = False
self.compression = False
self.ciphers = Noneif knownhosts isNone:
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)passelse:
pass# if len(self.hostkeys.items()) == 0:# raise HostKeysException('No Host Keys Found')
Solution 3:
Also I am using version 0.2.9 and have the same issue.
If your only concern is the warning message (that in my case keeps showing up also explicitly setting hostkeys = none
) and you only want to get rid of it without messing with the code of the module, you can use the appropiate method filterwarnings of the module warnings that's used for print the message.
This will suppress all the warnings
import warnings
warnings.filterwarnings('ignore')
This instead will suppress all the warning messages that begin with "Failed to load HostKeys" that, i believe, is only that one that is happening in this case:
import warnings
warnings.filterwarnings('ignore','.*Failed to load HostKeys.*')
Post a Comment for ""failed To Load Hostkeys" Warning While Connecting To Sftp Server With Pysftp"