#!/usr/bin/perl # # $Id$ # # kill_known_host: Remove an entry from the ssh hosts file. # Usage: kill_known_host # kill_known_host # Exits: 0 on success. # 1 on error. # 111 if the entry didn't exist. # Notes: Based on Jim's kill-known-host. # my $KNOWN_HOSTS = "$ENV{HOME}/.ssh/known_hosts"; my $SCRATCH = "$KNOWN_HOSTS.$$"; # Check args. unless (@ARGV) { print STDERR "Usage: kill_known_host \n"; print STDERR "Usage: kill_known_host \n"; print STDERR "Exits: 0 on success.\n"; print STDERR " 1 on error.\n"; print STDERR " 111 if the entry didn't exist.\n"; exit 1; } my $host = shift; my $re; # Check if it's an IP or a hostname. if ($host =~ /^[1-9]\d{0,2}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { $re = "^([^\\s]+,)?$host\\s"; } else { $re = "^$host\\s"; } # Read known hosts. open IN, "< $KNOWN_HOSTS" or die "Can't read known_hosts: $!\n"; # Open scratch file. open OUT, "> $SCRATCH" or die "Can't open scratch file: $!\n"; my ($in, $out) = (0, 0); while () { $in++; next if /$re/; print OUT; $out++; } # Cleanup. close OUT; close IN; # Move hosts. rename $SCRATCH, $KNOWN_HOSTS or die "Can't rename $KNOWN_HOSTS: $!\n"; exit 111 if $in == $out; exit 0;