python3.7使用paramiko包连接远程主机报错:paramiko.ssh_exception.SSHException: could not get keys from ssh-agent
具体报错:
Traceback (most recent call last):
File "ssh_test.py", line 79, in test_ssh
ssh_client = get_ssh_client(vm_ip, vm_username, vm_password, vm_port)
File "ssh_test.py", line 52, in wrapper
result = func(*args, **kwargs)
File "ssh_test.py", line 69, in get_ssh_client
ssh_client.connect(host_ip, port=host_port, username=host_username, password=host_password, look_for_keys=False)
File "/usr/local/python3/lib/python3.7/site-packages/paramiko/client.py", line 496, in connect
passphrase,
File "/usr/local/python3/lib/python3.7/site-packages/paramiko/client.py", line 745, in _auth
self._agent = Agent()
File "/usr/local/python3/lib/python3.7/site-packages/paramiko/agent.py", line 415, in __init__
self._connect(conn)
File "/usr/local/python3/lib/python3.7/site-packages/paramiko/agent.py", line 85, in _connect
raise SSHException("could not get keys from ssh-agent")
paramiko.ssh_exception.SSHException: could not get keys from ssh-agent
During handling of the above exception, another exception occurred:
解决方法:
使用 SSHClient.connect 的 allow _ agent=False 参数避免 Paramiko 尝试与其联系
# 获取ssh客户端
@cost_time
def get_ssh_client(host_ip, host_username, host_password, host_port=22):
print_have_time("--------------------------获取SSH客户端--------------------------")
print_have_time(f"host_ip: {host_ip}, host_username: {host_username}, host_password: {host_password}, host_port: {host_port}")
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(host_ip, port=host_port, username=host_username, password=host_password, allow_agent=False)
return ssh_client