-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-hosthashes
executable file
·60 lines (44 loc) · 1.6 KB
/
ssh-hosthashes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/perl
=head1 NAME
ssh-hosthashes - List duplicate entries in ~/.ssh/known_hosts by pubkey
=head1 SYNOPSIS
This will display a list of SSH hosts arranged in clusters by public key
hash, including line numbers for each entry. You can then consolidate these
entries by comma-separating them on the same line.
Arguments are assumed to be file(s) to scan. No options are currently
supported. You can alternatively pipe a known_hosts equivalent to
ssh-hosthashes, e.g.
sudo cat /home/some_user/.ssh/known_hosts |ssh-hosthashes
ssh-hosthashes 0.2.20160312.1 Copyright 2010+ by Adam Katz, GPLv2+/BSD 2-clause
=head1 AUTHORS
Adam Katz E<lt>https://github.com/adamhotep/net-scriptsE<gt>
=cut
use warnings;
use strict;
if ( $ARGV[0] && $ARGV[0] =~ /^--?h/ ) {
use Pod::Usage "pod2usage";
pod2usage({-verbose=>2, -exitval=>0, -output=>\*STDOUT});
}
# filename, dash for standard input, or default to using ~/.ssh/known_hosts
if (defined($ARGV[0])) {
if ($ARGV[0] =~ /^-$/) { open (KNOWN_HOSTS, "cat |") or die $!; }
else { open (KNOWN_HOSTS, $ARGV[0]) or die $!; }
} else { open (KNOWN_HOSTS, "< $ENV{HOME}/.ssh/known_hosts") or die $!; }
my %hash = ();
while (<KNOWN_HOSTS>) {
next unless /^(.+) (ssh-\S+\s.+)/;
$hash{$2} .= "$.: $1\n\t";
}
my $col = 80;
if ( defined($ENV{COLUMNS}) ) { $col = $ENV{COLUMNS}; }
elsif (open (RESIZE, "resize 2>/dev/null |") ) {
while (<RESIZE>) { next unless /COLUMNS=(\d+)/; $col = $1; last; }
close (RESIZE);
}
$col -= 3;
foreach (keys %hash) {
next unless $hash{$_} =~ /\n../;
my $host = $hash{$_};
s/^(.{$col}...).{4,}(.{$col})$/$1...$2/;
print "$_\n\t$host\n";
}