Home >Backend Development >Python Tutorial >Paramiko 'Unknown Server' Exception: How to Fix It?
Paramiko "Unknown Server" Exception: Troubleshooting and Resolution
Paramiko, a popular Python library, provides an interface for secure SSH communication. However, when attempting to connect to a server using Paramiko, users may encounter the "Unknown Server" exception. This exception arises when the server's host key is not known to the client.
Understanding the Problem
The "Unknown Server" exception occurs when the SSH client attempts to connect to a server whose host key is not stored in either the system or local HostKeys objects. By default, Paramiko uses a policy that rejects all unknown servers (RejectPolicy).
Resolution
To resolve this issue, one can modify the policy used when connecting to unknown servers. Here's how:
Import Paramiko:
import paramiko
Create an SSH Client:
client = paramiko.SSHClient()
Set AutoAddPolicy:
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
Connect to the Server:
client.connect('127.0.0.1', username=username, password=password)
By using AutoAddPolicy, the SSH client automatically adds the host key to its known hosts list. This allows the connection to succeed even if the server's host key is not previously known.
Additional Notes
By implementing these steps, users can overcome the "Unknown Server" exception and establish successful SSH connections using Paramiko.
The above is the detailed content of Paramiko 'Unknown Server' Exception: How to Fix It?. For more information, please follow other related articles on the PHP Chinese website!