Paramiko “Unknown Server”

前端 未结 5 2112
余生分开走
余生分开走 2020-11-22 15:13

I\'m trying to get started with the Paramiko library, but the library is throwing an exception as soon as I try to connect with the following simple program:



        
5条回答
  •  囚心锁ツ
    2020-11-22 15:54

    The correct way is either:

    • Call the HostKeys.add on the instance returned by SSHClient.get_host_keys before calling the connect, passing it the trusted key.

        from base64 import decodebytes
        # ...
      
        keydata = b"""AAAAB3NzaC1yc2EAAAADAQAB..."""
        key = paramiko.RSAKey(data=decodebytes(keydata))
        client.get_host_keys().add('example.com', 'ssh-rsa', key) 
      

      To see how to obtain the fingerprint for use in the code, see my answer to:
      Verify host key with pysftp.

      If you know a fingerprint only, see:
      Python - pysftp / paramiko - Verify host key using its fingerprint

    • Or load the already cached hostkey (e.g. by the command-line ssh) using client.load_system_host_keys().

    • Or you can at least cache the hostkey from the first attempt to make sure it does not change in the future.

      For that use SSHClient.load_host_keys before connect. It makes Paramiko automatically add the new host key to the file (when combined with AutoAddPolicy).

提交回复
热议问题