Reputation: 95
I'm using paramiko and I need the host key of an ssh server in base 64 for the following line:
key = paramiko.RSAKey(data=base64.decodestring('...'))
Does anyone know of a way either through the Mac OS X terminal, in a python script, or something else to find this? Thanks
Upvotes: 1
Views: 5457
Reputation: 567
>>> from paramiko.client import SSHClient
>>> client = SSHClient()
>>> client.connect('hostname')
# This probably will fail since there's no auth set here
>>> rsa_key = client.get_transport().get_remote_server_key()
<paramiko.rsakey.RSAKey object at 0x109305e90>
# however, this still exists
>>> rsa_key.get_base64()
u'AAAAB3NzaC1y...Nhd'
Since you're using paramiko already, you probably will want to just stop at rsa_key and not make a new one with get_base64() since rsa_key is of RSAKey type already.
A server can define a non-RSA key as well (or multiple) but if you know that it only returns RSA, this will obviously always return RSA. It will depend on which key is negotiated.
Upvotes: 1
Reputation: 168726
You can retrieve the server's public key from the server itself, without having to authenticate yourself to the server.
import paramiko
import socket
import sys
for arg in sys.argv[1:]:
sock = socket.socket()
sock.connect((arg, 22))
trans = paramiko.transport.Transport(sock)
trans.start_client()
k = trans.get_remote_server_key()
# On my machine, this returns a paramiko.RSAKey
print k.get_base64()
Upvotes: 4
Reputation: 65
Do you need to do this only once, or do you need to be able to programmatically grab the host key at runtime? If only once,
ssh -v <hostname>
will show you the host key.
Upvotes: 1