如何在Python中ssh到user@host?

问题描述 投票:0回答:1
import paramiko

host = "172.21.14.48"
port = 22
username = "user"
password = "xxx*9841"
command = "ls"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password )
stdin, stdout, stderr = ssh.exec_command(command)
lines = stdout.readlines()
print(lines)

此代码可以在 SSH 上成功运行。但我需要使用相同的用户名和密码 ssh 到 [email protected]

Using username "cp".
Keyboard-interactive authentication prompts from server:
Actual Username:
Actual Password:

我尝试了下面的代码,但出现错误。

host = "[email protected]"

错误消息为:

文件“C:\Program Files\Python38\lib\socket.py”,行 918,在 getaddrinfo 中
对于_socket.getaddrinfo(主机,端口,系列,类型,原型,标志)中的资源:socket.gaierror:[Errno 11003] getaddrinfo失败

但是使用 PuTTY 我可以 ssh 到 [email protected]。有什么建议吗..?

python ssh paramiko
1个回答
1
投票

cp
中的
ssh [email protected]
用户名,所以它转到
username
SSHClient.connect
参数:

username = "cp"
# ...
ssh = paramiko.SSHClient()
ssh.connect(host, port, username, password)

您的服务器使用键盘交互式身份验证提示输入另一组凭据。
为此,请参阅:Python Paramiko 中的密码身份验证失败,但相同的凭据在 SSH/SFTP 客户端中有效
您必须调整代码以回答第二个用户名和密码提示。处理程序的

fields
参数将有两个条目,并且处理程序必须在其结果中返回两个答案。

© www.soinside.com 2019 - 2024. All rights reserved.