DEV Community

Jay
Jay

Posted on • Originally published at jay.gooby.org on

uknown-host

I updated a server cluster last night and was getting loads of

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Enter fullscreen mode Exit fullscreen mode

warnings. You get told about the offending line number:

Offending RSA key in ~/.ssh/known_hosts:531

Enter fullscreen mode Exit fullscreen mode

And usually I’d just edit it and be done, but after doing it a couple of times I wrote unknown-host

#!/bin/bash
#
# Use this to remove the offending line from your
# ~/.ssh/known-hosts file when you know the host
# has actually changed and you get the
#
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#
# Offending RSA key in ~/.ssh/known_hosts:531
#
# warning telling you what line needs removing.
#
# usage: unknown-host <line number>
#
# So calling:
#
# unknown-host 531
#
# will remove line 531 and then you can ssh again with the nag.
#
# jay@gooby.org
# @jaygooby

line=$1

# number detection via https://stackoverflow.com/a/3951175/391826
case $line in
    ''|*[!0-9]*) line="" ;;
esac

if [ -z "$line" ]; then
  echo -e "Usage: $(basename $0) <line number>" >&2
  exit 1
else
  sed -i"" -e "${line}d" ~/.ssh/known_hosts
fi
Enter fullscreen mode Exit fullscreen mode

so I wouldn’t have to do that again, in the great spirit of xkcd’s “Automation” webcomic

xkcd webcomic about how writing a simple script to automate a task grows exponentially

Top comments (0)