How to use paramiko.RSAKey.from_private_key ()?

I'm new to paramiko, any idea how I can use the paramiko.RSAKey.from_private_key () function?

I know there is from_private_key_file (), but I'm interested in using a function to parse a private key (like below) and using this private key for SSHClient.

Private key (sample)

-----BEGIN RSA PRIVATE KEY-----\nMIICXgIBAAKCAIEAmfgmlY95SHXhCeBNdkhSrsG4JVbqyew845yoZRX3wcS2/doz\niVQxgx0aiOwLi+/Rnkb3PLUIwoxb/LoD/W0YMS6/NSUMt+LdH+zsjeNF2iq4rDzU\nwDSqi27q/8u/egrK7H+9HNKEVXb/87utAAm3VTM9KqKaK3VuVFrNrnsDSuECAwEA\nAQKCAIBZn3y2KiGq8BLiMNJmO4sFdnW+Jm3cw8pdo17SGItzGxJ5iX3ePkfjzhkY\nAm5mMl6OBzj6+VX0CMeywIR6C/q8HwDYSmZcuU5v76/DoW5bI6xkPrroqEz6aRE5\nyN+2hf65RD3eoPATsdrP/kxiKjZg9uG9LhgIXyVwYFs1RcqewQJBAMCVJlEYXRio\neynUtyES9HNmUGUqHKmri1FZfO56/mFdG5ZXsKE48qURCAGVxI+goGQ4vtJIXB2J\nyTEr+5qYtE0CQQDMq9/iigk+XDOa9xGCbwxbLGdPawaEivezMVdPqVzH971L6kZ8\nhEnev1DqujgGCyR+QYPW1ZCXH05FY9CqWwrlAkATzYJyJlI0XebER2ZJVVyjnSq5\nLFpkLAqYY95P23/a3SsgC4ZTHbr9tEGhgBgFONwlUhx1HRGzy95PWxl1LSylAkBk\nwP93v8gJIM5urM27zfrhLxy0ZdVRji+d0N5QYuk/r19KbcvBJEZRFxE4W++UWgve\n81V5fqytGEYptpdUJXlZAkEArxZDiT1HXXGciIgzZbh53McogPCGHiKOOPSjpM41\npneDFVvwgezCWoDauxNDzu7Nl55qPJsmvfKZ+SKvCajrhQw==\n-----END RSA PRIVATE KEY-----\n 

The code I wanted to run:

 import paramiko ssh = paramiko.SSHClient() # how do I pass in the private_key, when my private_key (shown above) is in string? mykey = paramiko.RSAKey.from_private_key(private_key) ssh.connect('192.168.1.2', username = 'vinod', pkey = mykey) 

Many thanks.

+8
python paramiko
source share
5 answers

from_private_key() obviously accepts a file object:

from_private_key (cls, file_obj, password = None)

Create a key object by reading the private key from a file (or file). If the secret key is encrypted and the password is not None, this password will be used to decrypt the key (otherwise, a PasswordRequiredException will be thrown).

Options:

file_obj (file) - file to read from password (str) - optional password to decrypt the key, if it is encrypted

Returns: PKey

new key object based on this private key

Raises:

IOError - if an error occurred while reading the key

PasswordRequiredException - if the secret key file is encrypted and the password is None

SSHException - if the key file is invalid

So, to pass it as a string, you can use StringIO , something like:

 private_key = StringIO.StringIO(key_string) mykey = paramiko.RSAKey.from_private_key(private_key) 

I have not tested this.

+4
source share

The Leo method worked for me:

 >>> import paramiko >>> f = open('/path/to/key.pem','r') >>> s = f.read() >>> import StringIO >>> keyfile = StringIO.StringIO(s) >>> mykey = paramiko.RSAKey.from_private_key(keyfile) >>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) >>> ssh.connect('myserver.compute-1.amazonaws.com', username='ubuntu', pkey=mykey) >>> stdin, stdout, stderr = ssh.exec_command('uptime') >>> stdout.readlines() [' 19:21:10 up 24 days, 42 min, 1 user, load average: 0.14, 0.06, 0.05\n'] 
+4
source share

This is where the duck seal comes in handy - it doesn't need to be a duck (= file), it just has to WIN, as one.

A little experimentation shows that any object that has a valid readlines () method is good.

I faked it with:

 def myfakefile(keystring): myfakefile.readlines=lambda: keystring.split("\n") return myfakefile mykey = paramiko.RSAKey.from_private_key(myfakefile(keystring)) 

This is incredibly hacked, but it works.

What does this mean when you call myfakefile (keystring), it creates myfakefile.readlines, which returns the (divided) contents of the keys.

Then it returns a function.

The same functions are passed to the from_private_key command. from_private_key, thinking it is a file, calls myfakefile.readlines (). This calls the newly created (lambda) function, which returns the kind of thing you expect from file.readlines () - or, close enough, anyway.

Please note that saving results will not work as expected:

 k1=myfakefile(keystring1) k2=myfakefile(keystring2) # This will return keystring2, not keystring1! paramkiko.RSAKey.from_private_keyfile(k1.readlines()) 

There are more reliable ways to make it work as it should, but it’s not worth the effort - just use StringIO if your needs are more complex.

+1
source share

This should do it:

 import io import paramiko private_key_file = io.StringIO() private_key_file.write('-----BEGIN RSA PRIVATE KEY-----\nlskjdflk\n...\n-----END RSA PRIVATE KEY-----\n') private_key_file.seek(0) private_key = paramiko.RSAKey.from_private_key(private_key_file) 
+1
source share

A very old question, but in case it helps some unfortunate soul: my problem is to create a new key with default settings using

ssh-keygen -t rsa

My previous key was generated using

ssh-keygen -t rsa -b 4096 -a 100

who paramiko complained as well as on the OP.

-one
source share

All Articles