def _sftp_connect(self): """Establish the SFTP connection.""" if not self._sftp_live: self._sftp = paramiko.SFTPClient.from_transport(self._transport) if self._auto_add_key == True: hostkeys = self._cnopts.hostkeys self._cnopts.hostkeys = None hostkeys.add(self._host, self.remote_server_key.get_name(), self.remote_server_key) hostkeys.save(known_hosts()) if self._default_path is not None: # print("_default_path: [%s]" % self._default_path) self._sftp.chdir(self._default_path) self._sftp_live = True
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) else: if len(self.hostkeys.items()) == 0: raise HostKeysException('No Host Keys Found')
import os from pysftp.helpers import known_hosts from paramiko.hostkeys import HostKeys if __name__ == '__main__': h = HostKeys() try: kh = known_hosts() print("known_hosts:", kh) h.load(os.path.expanduser(kh)) except Exception as e: print(e) print(len(h.items()))