Retrieving public key through SSHClient
Closed this issue · 2 comments
Can we retrieve the public key of the private key in any of the RSA/DSA/ECDSA formats through SSHClient? I tried
ssh_client=paramiko.SSHClient()
ssh_client._key_from_filepath("/root/id_dsa", paramiko.DSSKey, None)
but it is throwing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "paramiko/client.py", line 512, in _key_from_filepath
self._log(DEBUG, msg)
File "paramiko/client.py", line 686, in _log
self._transport._log(level, msg)
AttributeError: 'NoneType' object has no attribute '_log'
error. Is it a bug or did I wrongly execute the command?
Use triple-backticks on their own lines in order to quote code and terminal output, like this:
```
paste your code here
lines are properly preserved
put triple-backticks alone on their own lines
```
You should not try to use methods/members that start with a leading underscore, like _key_from_filepath()
. The leading underscore _
means that it is only intended to be used internally.
In a private email, you asked how to generate a typical public key string for a DSS key. Here is how:
>>> from paramiko import DSSKey
>>> key = DSSKey.from_private_key_file("test_dss.key")
>>> print("ssh-dss " + key.get_base64())
ssh-dss AAAAB3NzaC1kc3MAAACBAOeBpgNnfRzr/twmAQRu2XwWAp3CFtrVnug6s6fgwj/oLjYbVtjAy6pl/h0EKCWx2rf1IetyNsTxWrniA9I6HeDj65X1FyDkg6g8tvCnaNB8Xp/UUhuzHuGsMIipRxBxw9LF608EqZcj1E3ytktoW5B5OcjrkEoz3xG7C+rpIjYvAAAAFQDwz4UnmsGiSNu5iqjn3uTzwUpshwAAAIEAkxfFeY8P2wZpDjX0MimZl5wkoFQDL25cPzGBuB4OnB8NoUk/yjAHIIpEShw8V+LzouMK5CTJQo5+Ngw3qIch/WgRmMHy4kBq1SsXMjQCte1So6HBMvBPIW5SiMTmjCfZZiw4AYHK+B/JaOwaG9yRg2Ejg4Ok10+XFDxlqZo8Y+wAAACARmR7CCPjodxASvRbIyzaVpZoJ/Z6x7dAumV+ysrV1BVYd0lYukmnjO1kKBWApqpH1ve9XDQYN8zgxM4b16L21kpoWQnZtXrY3GZ4/it9kUgyB7+NwacIBlXa8cMDL7Q/69o0d54U0X/NeX5QxuYR6OMJlrkQB7oiW/P/1mwjQgE=
There is currently no good way to load any ssh key of any type - you need to know the type, or try all of the types. Something like this perhaps:
key = None
for pkey_class in (RSAKey, DSSKey, ECDSAKey, Ed25519Key):
try:
key = pkey_class.from_private_key_file(filename)
break
catch Exception:
continue
...
@ploxiln Thanks. SSHClient's method seemed to be handling key parsing for all the different key types directly, so I wanted to see if I can try it.