Error in paramiko: paramiko.transport: TypeError: long() argument must be a string or a number, not 'NoneType'

Error in paramiko: paramiko.transport: TypeError: long() argument must be a string or a number, not 'NoneType'

正在尝试构建一个自包含的 python 脚本以通过带有私钥文件的 paramiko 连接到服务器并且 known_hosts 存储在本地目录中。它已连接,但一直出现错误:

paramiko.transport: TypeError: long() argument must be a string or a number, not 'NoneType'

我在文档或代码中找不到任何关于这里发生的事情的信息。

client = paramiko.SSHClient()
paramiko.util.log_to_file('paramiko.log')
client.load_system_host_keys('known_hosts')
client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.get_host_keys().add(hostname, 'ssh-rsa', key)
client.connect(hostname, username='root', pkey=key)
print '\tconnected'
self.stdin, self.stdout, self.stderr = client.exec_command('/usr/local/sbin/comd')

client.close()

日志:

DEB [20150123-12:33:23.334] thr=1   paramiko.transport: Switch to new keys ...
DEB [20150123-12:33:23.342] thr=2   paramiko.transport: Trying SSH key 992b10a56fa369fcc7d61b64f08a3d53
DEB [20150123-12:33:23.553] thr=1   paramiko.transport: userauth is OK
ERR [20150123-12:33:23.553] thr=1   paramiko.transport: Unknown exception: long() argument must be a string or a number, not 'NoneType'
ERR [20150123-12:33:23.555] thr=1   paramiko.transport: Traceback (most recent call last):
ERR [20150123-12:33:23.556] thr=1   paramiko.transport:   File "/usr/lib/python2.7/site-packages/paramiko/transport.py", line 1625, in run
ERR [20150123-12:33:23.556] thr=1   paramiko.transport:     self.auth_handler._handler_table[ptype](self.auth_handler, m)
ERR [20150123-12:33:23.556] thr=1   paramiko.transport:   File "/usr/lib/python2.7/site-packages/paramiko/auth_handler.py", line 241, in _parse_service_accept
ERR [20150123-12:33:23.556] thr=1   paramiko.transport:     sig = self.private_key.sign_ssh_data(blob)
ERR [20150123-12:33:23.556] thr=1   paramiko.transport:   File "/usr/lib/python2.7/site-packages/paramiko/rsakey.py", line 97, in sign_ssh_data
ERR [20150123-12:33:23.556] thr=1   paramiko.transport:     rsa = RSA.construct((long(self.n), long(self.e), long(self.d)))
ERR [20150123-12:33:23.557] thr=1   paramiko.transport: TypeError: long() argument must be a string or a number, not 'NoneType'
ERR [20150123-12:33:23.557] thr=1   paramiko.transport:

请检查 pkey 是否确实包含您的私钥。如果没有,用

加载它
pkey = paramiko.RSAKey.from_private_key_file("priv.key")

另请注意,"host_keys" 包含远程主机的 public 键列表。您的代码实际上将您的私钥推送到用于主机密钥验证的 "known_hosts" 数据库中。请删除此行或在此处添加远程主机公钥。

client.get_host_keys().add(hostname, 'ssh-rsa', peer_pubkey)