如何自动点 yes [root@test-01 tmp]# ssh tkamc@'10.10.8.12' The authenticity of host '10.10.8.12 (10.10.8.12)' can't be established. ECDSA key fingerprint is SHA256:9AG2i7hh1B3t0vnNT4oEAoyFi66cfP/DQaWY6XFDidY. ECDSA key fingerprint is MD5:c2:42:8c:dd:a0:d0:c7:3a:a7:aa:6e:72:87:44:9a:f9. Are you sure you want to continue connecting (yes/no)?
时间: 2024-04-11 08:28:01 浏览: 13
要在 Python 中自动回答 "yes" 来确认 SSH 连接指纹验证,可以使用 `paramiko` 库中的 `AutoAddPolicy` 并结合 `SSHClient` 的 `connect()` 方法的 `look_for_keys` 参数。以下是一个示例:
```python
import paramiko
def ssh_auto_yes(remote_host, remote_port, remote_username, remote_password):
try:
# 创建 SSH 客户端对象
client = paramiko.SSHClient()
# 自动添加主机密钥
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器并自动回答 "yes"
client.connect(remote_host, port=remote_port, username=remote_username, password=remote_password, look_for_keys=False)
# 连接成功后的操作
# ...
# 关闭连接
client.close()
return True
except paramiko.AuthenticationException:
print(f"无法使用提供的用户名和密码登录到服务器 {remote_host}")
except paramiko.SSHException as ssh_exception:
print(f"SSH 连接错误: {ssh_exception}")
except Exception as e:
print(f"发生错误: {e}")
return False
# 调用函数进行自动回答 "yes" 并连接SSH
can_connect = ssh_auto_yes("10.10.8.12", 22, "tkamc", "密码")
if can_connect:
print("SSH 连接成功")
else:
print("SSH 连接失败")
```
在以上示例中,我们使用 `paramiko` 库创建一个 SSH 客户端对象,并将 `AutoAddPolicy` 设置为自动添加主机密钥。然后在调用 `connect()` 方法时,将 `look_for_keys` 参数设置为 `False`,以便禁用使用默认密钥文件进行验证。这样,在 SSH 连接时,将自动回答 "yes" 来确认指纹验证。
请确保将 "10.10.8.12"、端口、"tkamc" 和 "密码" 替换为实际的服务器和登录凭据。如果 SSH 连接成功,将输出 "SSH 连接成功";如果连接失败,将输出 "SSH 连接失败"。