Error: Paramiko "Unknown Server" Exception
When attempting to initiate a connection using the Paramiko library, users may encounter an "Unknown Server" exception. This occurs regardless of the target server address.
Resolution:
To resolve this issue, adjust the host key verification policy:
Import the paramiko library:
import paramiko
Create an SSH client instance:
client = paramiko.SSHClient()
Use set_missing_host_key_policy() to set the policy for handling unknown hosts:
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
Attempt to connect securely to the target server:
client.connect('127.0.0.1', username=username, password=password)
Optionally, execute commands:
stdin, stdout, stderr = client.exec_command('ls -l')
This policy allows you to automatically add unknown host keys to the system's SSH configuration for future reference.
Additional Tips:
Save the host key to a file for later use:
ssh.get_host_keys().save('/some/file/path')
Load the host key from a file for future connections:
ssh.load_host_keys('/some/file/path')
The above is the detailed content of Why Am I Getting an 'Unknown Server' Exception with Paramiko, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!