ramobal
ramobal

Reputation: 261

Paramiko PKey.from_private_key_file gives me "__init__() got an unexpected keyword argument 'filename'"

I get very weird behaviour from 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'

here it tells me I need a filename, but then whenever I try to specify anything:

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'

Can anyone explain to me what's happening? I'm completely confused.

Upvotes: 4

Views: 2929

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202088

  1. PKey.from_private_key_file is a class method.
  2. Do not use PKey base class directly. You have to use the correct descendant class, like RSAKey, DSSKey, etc.

As the documentation says:

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.

The correct code is like:

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

Though if you are going to use the key with SSHClient, you can pass the filename directly to key_filename argument of SSHClient.connect and you do not have to deal with key loading at all.

Upvotes: 5

Related Questions