Python - Paramiko Getting error "object has no attribute "get_fingerprint"

15,567

Solution 1

The pkey parameter should be the actual private key, not the name of the file containing the key. Note that pkey should be a PKey object and not a string (e.g. private_key = paramiko.RSAKey.from_private_key_file (private_key_filename) ). Instead of pkey you could use the key_filename parameter to pass the filename directly.

See the documentation for connect.

Solution 2

If you have your private key as a string, you can this on python 3+

from io import StringIO
ssh = paramiko.SSHClient()  

private_key = StringIO("you-private-key-here")
pk = paramiko.RSAKey.from_private_key(private_key)

ssh.connect('somehost.com', username='myName', pkey= pk)

Particularly useful if your private key is stored in an environment variable.

Share:
15,567
phileas fogg
Author by

phileas fogg

Updated on June 18, 2022

Comments

  • phileas fogg
    phileas fogg almost 2 years

    decided to give Python a try for the first time, so sorry if the answer is obvious.

    I'm trying to create an ssh connection using paramiko. I'm using the below code:

    #!/home/bin/python2.7
    
    import paramiko
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    ssh.connect("somehost.com", username="myName", pkey="/home/myName/.ssh/id_rsa.pub")
    stdin, stdout, stderr = ssh.exec_command("ls -l")
    
    print stdout.readlines()
    ssh.close()
    

    Pretty standard stuff, right? Except I'm getting this error:

     ./test.py
    Traceback (most recent call last):
    File "./test.py", line 10, in <module>
    ssh.connect("somehost", username="myName", pkey="/home/myName/.ssh/id_rsa.pub")
    File "/home/lib/python2.7/site-packages/paramiko/client.py", line 327, in connect
    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
    File "/home/lib/python2.7/site-packages/paramiko/client.py", line 418, in _auth
    self._log(DEBUG, 'Trying SSH key %s' % hexlify(pkey.get_fingerprint()))
    AttributeError: 'str' object has no attribute 'get_fingerprint'
    

    What "str" object is it referring to? I thought I merely had to pass it the path to the RSA key but it seems to be wanting some object.