python - Paramiko PKey.from_private_key_file 给我 "__init__() got an unexpected keyword argument ' 文件名'”

标签 python ssh paramiko

我从 paramiko 得到了非常奇怪的行为:

bla=paramiko.pkey.PKey(msg=None,data=None).from_private_key_file()
TypeError                                 Traceback (most recent call last)
<ipython-input-32-13288f655ecf> in <module>
----> 1 paramiko.pkey.PKey(msg=None,data=None).from_private_key_file()

TypeError: from_private_key_file() missing 1 required positional argument: 'filename'

这里它告诉我我需要一个文件名,但是每当我尝试指定任何内容时:

bla=paramiko.pkey.PKey(msg=None,data=None).from_private_key_file('key')
                                 Traceback (most recent call last)
<ipython-input-33-5fa0cf9b6317> in <module>
----> 1 bla=paramiko.pkey.PKey(msg=None,data=None).from_private_key_file('key')

~/anaconda3/lib/python3.7/site-packages/paramiko/pkey.py in from_private_key_file(cls, filename, password)
    233         :raises: `.SSHException` -- if the key file is invalid
    234         """
--> 235         key = cls(filename=filename, password=password)
    236         return key
    237 

TypeError: __init__() got an unexpected keyword argument 'filename'

谁能给我解释一下这是怎么回事?我完全糊涂了。

最佳答案

  1. PKey.from_private_key_file是一个类方法。
  2. 不要使用 PKey base class直接地。您必须使用正确的后代类,例如 RSAKey , DSSKey

作为documentation说:

Through the magic of Python, this factory method will exist in all subclasses of PKey (such as RSAKey or DSSKey), but is useless on the abstract PKey class.

正确的代码是这样的:

key = paramiko.RSAKey.from_private_key_file('key')

尽管如果您打算将 key 与 SSHClient 一起使用,您可以将文件名直接传递给 SSHClient.connectkey_filename 参数而且您根本不必处理 key 加载。

关于python - Paramiko PKey.from_private_key_file 给我 "__init__() got an unexpected keyword argument ' 文件名'”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61375946/

相关文章:

ssh - 如何更改密码ssh连接?

Git 和 SSH,使用哪个 key ?

Python Paramiko - 运行命令

python - 如何将这个迭代函数写成递归函数?

python - 在 M 个列表槽中组织 N 个对象的每种方法

python - pd.qcut 的值为 inf(无穷大) ValueError : Bin edges must be unique:

python - 导入本地包时遇到问题

ssh - 实现 SELinux 后无法使用 SSH?

Python Multiprocessing.Pool 工作人员在使用 pool.map 时挂起

python - 如何使用 Paramiko 传递命令行 ssh 参数?