Paramiko有时会引发'AuthenticationException'

时间:2016-05-01 14:32:07

标签: python python-3.x paramiko

这是罕见的情况之一,它几乎不可能重现,但我已经看到它发生在20次中的4次。

这是我的open_session方法:

def open_session:
    self.session = paramiko.SSHClient()
    self.host = host
    self.username = username
    self.password = password

    self.session.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:
        self.session.connect(self.host, username=self.username, port=port, password=self.password, timeout=self.connect_timeout)
    except paramiko.ssh_exception.BadHostKeyException:
        message = "host key could not be verified"
        self.sys_conf.logger.warning(message)
        raise BadCredentials(message)
    except paramiko.ssh_exception.AuthenticationException:
        message = "authentication failed."
        self.sys_conf.logger.warning(message)
        raise BadCredentials(message)
    except (paramiko.ssh_exception.SSHException, socket.error) as e:
        message = "could not establish connection, an error occurred: {}".format(e)
        self.sys_conf.logger.warning(message)
        raise ConnectionFail(message)
    except socket.timeout:
        message = "could not establish connection, time's out!"
        self.sys_conf.logger.warning(message)
        raise ConnectionFail(message)

Paramiko为什么会这样做?

2 个答案:

答案 0 :(得分:1)

应该是:

self.session = paramiko.SSHClient()
self.host = host
self.username = username
self.password = password
self.session.load_system_host_keys() # u missed this
self.session.set_missing_host_key_policy(paramiko.AutoAddPolicy())

load_system_host_keys(文件名=无) 从系统(只读)文件加载主机密钥。使用此方法读取的主机密钥不会被save_host_keys保存回来。 poaramiko

答案 1 :(得分:0)

经过进一步调查,结果表明机器本身存在问题。我最初的问题中提到的代码很好,并且在另一台更稳定的机器上连续工作了300次。