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