Skip to content

Instantly share code, notes, and snippets.

@jordiclariana
Created October 23, 2019 14:47
Show Gist options
  • Save jordiclariana/5b6296d01cf526b42fd5381ac4df253c to your computer and use it in GitHub Desktop.
Save jordiclariana/5b6296d01cf526b42fd5381ac4df253c to your computer and use it in GitHub Desktop.
Get host SSH public key and its fingerprint. Prototype for an Ansible module
import binascii
import paramiko
import warnings
warnings.filterwarnings(action='ignore', module='.*paramiko.*')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
HOSTNAME = 'github.com'
try:
ssh.connect(hostname=HOSTNAME)
except paramiko.ssh_exception.AuthenticationException:
pass
keys = ssh.get_host_keys()
key = keys[HOSTNAME]['ssh-rsa']
print("{hostname} ssh-rsa {public_key}".format(hostname=HOSTNAME,
public_key=key.get_base64()))
print(binascii.hexlify(key.get_fingerprint()).decode('ascii'))
@vide
Copy link

vide commented Apr 27, 2021

back from the grave but this could be done in a simpler way:

$ ssh-keyscan github.com
# github.com:22 SSH-2.0-babeld-6006113c
github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment