Home > Backend Development > Python Tutorial > How to Resolve Paramiko's 'Unknown Server' Exception?

How to Resolve Paramiko's 'Unknown Server' Exception?

DDD
Release: 2024-12-04 09:13:12
Original
815 people have browsed it

How to Resolve Paramiko's

Paramiko Unknown Server Exception: Solved

When working with the Paramiko library, users may encounter the "Unknown Server" exception, which prevents connection attempts. This issue arises when attempting to connect to servers without host keys recorded in the system or local HostKeys objects.

Solution:

To resolve this exception, you need to set the policy to use when connecting to unknown servers. By default, Paramiko rejects all unknown servers, but you can override this behavior using the AutoAddPolicy.

Here's a revised Python code snippet that adds the AutoAddPolicy solution:

import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('127.0.0.1', username=username, password=password)
stdin, stdout, stderr = client.exec_command('ls -l')
Copy after login

By setting the AutoAddPolicy, Paramiko will automatically add the host key of the unknown server to its host key cache, allowing future connections without exception.

Additional Options:

  • Saving Host Keys: After successful connection, you can save the host key in a file for future reference:
ssh.get_host_keys().save('/some/file/path')
Copy after login
  • Loading Host Keys: To load host keys from a file for subsequent connections:
ssh.load_host_keys('/some/file/path')
Copy after login

Using these techniques, you can establish secure SSH connections with Paramiko, even when dealing with servers without registered host keys.

The above is the detailed content of How to Resolve Paramiko's 'Unknown Server' Exception?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template