From a4d6029c6ec67962e667eb92956384edc9c07c9a Mon Sep 17 00:00:00 2001 From: neingeist Date: Mon, 16 May 2016 12:25:36 +0200 Subject: [PATCH] Add a script to find dupes in known_hosts --- ssh_known_dupes | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100755 ssh_known_dupes diff --git a/ssh_known_dupes b/ssh_known_dupes new file mode 100755 index 0000000..0c0f4c7 --- /dev/null +++ b/ssh_known_dupes @@ -0,0 +1,23 @@ +#!/usr/bin/python +# Find duplicate keys in ~/.ssh/known_hosts +# +# Of course, we should have been more careful in the first place :) + +from __future__ import division, print_function +from collections import defaultdict +import os.path +import re + + +hostnames_per_key = defaultdict(list) + +with open(os.path.expanduser('~/.ssh/known_hosts')) as f: + for line in f: + m = re.match('(\S+)\s+(\S+\s+\S+)(.*)?', line) + if m: + (hostname, key, cruft) = m.groups() + hostnames_per_key[key].append(hostname) + +for key, hostnames in hostnames_per_key.iteritems(): + if len(hostnames) > 1: + print(hostnames)