Created
July 22, 2021 22:53
-
-
Save yoichi/c9ea5095f380b10bcf9e91f675b462ec to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
import argparse | |
import base64 | |
import hashlib | |
import hmac | |
import os | |
def match(line, hostname): | |
host_str = line.split(' ')[0] | |
if host_str.startswith('|'): | |
host_array = host_str.split('|') | |
salt = base64.b64decode(host_array[-2]) | |
result = hmac.digest(salt, hostname.encode(), hashlib.sha1) | |
return base64.b64encode(result) == host_array[-1].encode() | |
else: | |
return hostname in host_str.split(',') | |
def print_known_host_entry(hostname_or_ip, port): | |
if port != 22: | |
hostname_or_ip = f'[{hostname_or_ip}]:{port}' | |
with open('{}/.ssh/known_hosts'.format(os.getenv("HOME"))) as f: | |
for line in f.readlines(): | |
if match(line, hostname_or_ip): | |
print(line) | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='get host entry from ~/.ssh/known_hosts') | |
parser.add_argument('hostname_or_ip') | |
parser.add_argument('-p', type=int, default=22) | |
args = parser.parse_args() | |
print_known_host_entry(args.hostname_or_ip, args.p) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment