Created
October 23, 2019 14:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
back from the grave but this could be done in a simpler way: