Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
params['cnopts'] = cnopts
# If it's the first time we connect to this host
# disable hostkey validation, and add to known_hosts
hostkeys = None
if cnopts.hostkeys.lookup(self.sftp_host) is None:
hostkeys = cnopts.hostkeys
cnopts.hostkeys = None
sftp = pysftp.Connection(**params)
if sftp and hostkeys is not None:
hostkeys.add(
self.sftp_host,
sftp.remote_server_key.get_name(),
sftp.remote_server_key)
known_hosts = pysftp.helpers.known_hosts()
if not os.path.exists(os.path.dirname(known_hosts)):
os.makedirs(os.path.dirname(known_hosts))
hostkeys.save(known_hosts)
_logger.info(
'Permanently added "%s" (%s) to the list of '
'known hosts' % (
self.sftp_host,
sftp.remote_server_key.get_name()))
return sftp