Is there any way to generate public SSH key from private one using Python?
... or if not, can you provide me link or RFC number to full algorithm (from getting ASCII encoded Private key file/string to generation of ASCII encoded public key file/string).
To show you a开发者_开发百科 bigger picture - I started using a pylibssh2 library. This library has a method - userauth_publickey_fromfile (it raises NotImplementedException, but when you force launching python binding - it works). The problem is that this method wants private key (good) and public key (hell knows why). Of course, I can pass both of those keys as argumets to my functions, but I think it is unnecessary, and I don't want to expect one more variable just for that. So I want to generate public key from private one by myself.
Maybe I'm missing something in pylibssh2 or even in libssh2 itself (I can write bindings for that as well) ?
Thanks for any help!
Do you need the answer to be strictly python? You can do this with ssh-keygen -y -f privatekey
I don't know how you get userauth_publickey_fromfile()
working, but according to http://github.com/wallix/pylibssh2/blob/master/libssh2/session.py, this method expects paths of key files, not the keys themselves.
If the private key is yours, then both - the public and private key - should reside in your ~/.ssh directory.
Private and public keys can only be created in pairs. If you could manage to derive one of them from the other, an cryptographic universe would explode.
The public key must be made available to the other side of the SSH connection. Therefore this method expects it in addition to the private key.
import commands
cmd = "ssh-keygen -y -f %s" % (file)
return commands.getstatusoutput(cmd)[1]
精彩评论