Reputation: 1933
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.
Upvotes: 5
Views: 21757
Reputation: 5500
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
.
Upvotes: 22
Reputation: 11728
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.
Upvotes: 5