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')
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:
ssh.get_host_keys().save('/some/file/path')
ssh.load_host_keys('/some/file/path')
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!