| 
									
										
										
										
											2023-01-06 13:19:29 +01:00
										 |  |  | #!/usr/bin/python3 | 
					
						
							| 
									
										
										
										
											2016-05-16 12:25:36 +02:00
										 |  |  | # 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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 16:44:10 +01:00
										 |  |  | for key, hostnames in hostnames_per_key.items(): | 
					
						
							| 
									
										
										
										
											2016-05-16 12:25:36 +02:00
										 |  |  |     if len(hostnames) > 1: | 
					
						
							|  |  |  |         print(hostnames) |