Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 456
phileas fogg Avatar asked May 30 '12 23:05

phileas fogg


People also ask

How do I SSH into Paramiko and server in Python?

A Paramiko SSH Example: Connect to Your Server Using a Password. This section shows you how to authenticate to a remote server with a username and password. To begin, create a new file named first_experiment.py and add the contents of the example file. Ensure that you update the file with your own Linode's details.

What is Paramiko SSHClient ()?

SSHClient. A high-level representation of a session with an SSH server. This class wraps Transport , Channel , and SFTPClient to take care of most aspects of authenticating and opening channels. A typical use case is: client = SSHClient() client.

What is Paramiko library in Python?

Paramiko is a Python library that makes a connection with a remote device through SSh. Paramiko is using SSH2 as a replacement of SSL to make a secure connection between two devices. It also supports the SFTP client and server model.

What is Paramiko transport?

An SSH Transport attaches to a stream (usually a socket), negotiates an encrypted session, authenticates, and then creates stream tunnels, called channels , across the session. Multiple channels can be multiplexed across a single session (and often are, in the case of port forwardings).


2 Answers

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.

like image 104
johlo Avatar answered Oct 17 '22 00:10

johlo


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.

like image 25
FacePalm Avatar answered Oct 17 '22 00:10

FacePalm