From: Iain Patterson Date: Thu, 17 Aug 2006 15:40:50 +0000 (+0000) Subject: Added kill_known_host script and completion. X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=756cfebbb2ef97127f5d5c0ff2254c1e16441d9b;p=profile.git Added kill_known_host script and completion. git-svn-id: https://svn.cambridge.iain.cx/profile/trunk@31 6be0d1a5-5cfe-0310-89b6-964be062b18b --- diff --git a/.profile.d/completion.bashrc b/.profile.d/completion.bashrc index 68b1c9d..663b56d 100644 --- a/.profile.d/completion.bashrc +++ b/.profile.d/completion.bashrc @@ -26,6 +26,16 @@ function _process() { return 0 } +# Host completion for SSH known hosts. +function _known_hosts() { + cur=${COMP_WORDS[COMP_CWORD]} + + HOSTS=$(sed 's/[ ].*//;s/,/\n/' ~/.ssh/known_hosts) + + COMPREPLY=($(compgen -W "$HOSTS" -- "$cur")) + return 0 +} + # Process completion for kill, strace etc. function _pid() { cur=${COMP_WORDS[COMP_CWORD]} @@ -83,3 +93,4 @@ complete -F _make make complete -F _process killall complete -F _pid kill complete -F _pid strace +complete -F _known_hosts kill_known_host diff --git a/kill_known_host b/kill_known_host new file mode 100755 index 0000000..acc6385 --- /dev/null +++ b/kill_known_host @@ -0,0 +1,60 @@ +#!/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;