Python-Parmiko Error: 'RSAKey' object is not iterable'

Python-Parmiko Error: 'RSAKey' object is not iterable'

我通过 sudo ssh-keygen -t rsa -b 4096 创建了一个 RSA 密钥对并复制到 .ssh 文件夹。我正在使用 Python paramiko 包。我的代码是:

import paramiko
k = paramiko.RSAKey.from_private_key_file('/home/username/.ssh/id_rsa', password='mypassphrase')
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn = s.connect('remote_computer', username ='username', port=22, key_filename =k)
command = 'sudo python ~/pythonscript.py'
(stdin, stdout, stderr) = s.exec_command(command)
s.close()

但是当我尝试 运行 代码时,出现以下错误和回溯:

Traceback (most recent call last):
File "pythonparamikoscript.py", line 6, in <module>
conn = s.connect('remote_computer', username ='username', port=22, key_filename =k)
File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 307, in connect
look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)
File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 441, in _auth
for key_filename in key_filenames:
TypeError: 'RSAKey' object is not iterable

感谢您的帮助,因为这让我很绝望。

connect() 接受不同的参数:

  • pkey (PKey) 这是您应该分配密钥的地方
  • key_filename (str) 这只是一个包含您的密钥位置的字符串。

所以运行

conn = s.connect('remote_computer', username ='username', port=22, pkey=k)

应该完成这项工作。