paramiko中的错误:paramiko.transport:TypeError:long()参数必须是字符串或数字,而不是' NoneType'

时间:2015-01-23 17:46:52

标签: python ssh paramiko

尝试构建一个自包含的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:

1 个答案:

答案 0 :(得分:1)

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

pkey = paramiko.RSAKey.from_private_key_file("priv.key")

另请注意" host_keys"包含远程主机的公钥列表。您的代码实际上将您的私钥推送到" known_hosts"用于主机密钥验证的db。请删除此行或在那里添加远程主机pubkey。

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