0

I would like to know whether it is possible to use SSH key pair without saving private key file permanently anywhere (it is not possible to use user/password authentication). After generating it (to the temp folder) with:

from paramiko import RSAKey
bits = 2048
filename='output'
#Private key
prv = RSAKey.generate(bits=bits)
prv.write_private_key_file(filename, password='')

#Public key
pub = RSAKey(filename=filename, password='')
with open("%s.pub" % filename, "w") as f:
    f.write("%s %s" % (pub.get_name(), pub.get_base64()))

Can one provide the public key to the remove server owner to add it to his known keys and then while attempting to join to his server use some kind of mechanism that does not require having private key stored permanently anywhere?

Thanks for letting me know that.

New2coding
  • 715
  • 11
  • 23
  • Yes, that is what I meant, could you please provide an example? Assume the public key is obtained as pub.get_base64(). It should be provided to the host but from_private_key_file() method used to return correct object that can be passed as a pkey argument to connect() method will not accept any hardcoded piece? – New2coding Aug 31 '18 at 11:20
  • What is the way of converting my private key ('prv' in the example above) to string that can be used as in the post you mentioned above. I only found out this method: pub.get_base64() that can be used for public keys. Thanks – New2coding Aug 31 '18 at 12:03
  • 1
    `prv.write_private_key_file` gets you the key in that format (if you remove the `password` argument). – Martin Prikryl Aug 31 '18 at 12:06
  • if I do prv = RSAKey.generate(bits=bits) and then prv.write_private_key_file(filename) and then print prv, it returns something (ϸˆWNµ?Þgñ虞...) that cannot be inserted into the write() method. If I do prv.get_base64() it returns the same as pub.get_base()? – New2coding Aug 31 '18 at 12:26
  • The **contents of the file** created with `write_private_key_file` is what you need. – Martin Prikryl Aug 31 '18 at 12:29
  • I understand but I am newb in this area, I do not know how to access the contents of the file (prv). My aim is to print the contents so that I can copy it and paste it afterwards. Thanks – New2coding Aug 31 '18 at 12:47
  • 1
    Are you looking for this - [Python: printing a file to stdout](https://stackoverflow.com/q/8084260/850848)? – Martin Prikryl Aug 31 '18 at 12:50
  • Yes, that is what I was looking for. Thanks! – New2coding Aug 31 '18 at 12:53

0 Answers0