3 # kill_known_host: Remove an entry from the ssh hosts file.
4 # Usage: kill_known_host [options] <IP>|<hostname>
5 # Options: -n Only report key; don't delete it.
6 # -p Allow partial match.
7 # Example: kill_known_host -p www
8 # Remove all keys with hostname beginning www.
11 # 111 if the entry didn't exist.
12 # Notes: Based on Jim's kill-known-host.
17 my $KNOWN_HOSTS = "$ENV{HOME}/.ssh/known_hosts";
18 my $SCRATCH = "$KNOWN_HOSTS.$$";
25 print STDERR "Usage: kill_known_host [options] <IP>|<hostname>\n";
26 print STDERR "Options: -n Only report key; don't delete it.\n";
27 print STDERR " -p Allow partial match.\n";
28 print STDERR "Example: kill_known_host -p www\n";
29 print STDERR " Remove all keys with hostname beginning www.\n";
30 print STDERR "Exits: 0 on success.\n";
31 print STDERR " 1 on error.\n";
32 print STDERR " 111 if the entry didn't exist.\n";
37 my $dotstar = $opt_p ? '[^\s,]*' : '';
40 # Check if it's an IP or a hostname.
41 if ($host =~ /^[1-9]\d{0,2}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
42 $re = "^([^\\s]+,)?$host$dotstar\\s";
45 $re = "^$host$dotstar\[\\s,]";
49 open IN, "< $KNOWN_HOSTS" or die "Can't read known_hosts: $!\n";
52 open OUT, "> $SCRATCH" or die "Can't open scratch file: $!\n" unless $opt_n;
54 my ($in, $out) = (0, 0);
79 rename $SCRATCH, $KNOWN_HOSTS or die "Can't rename $KNOWN_HOSTS: $!\n";
81 exit 111 if $in == $out;