Skip to content

Instantly share code, notes, and snippets.

@slumos
Created February 7, 2012 17:57
Show Gist options
  • Save slumos/1760997 to your computer and use it in GitHub Desktop.
Save slumos/1760997 to your computer and use it in GitHub Desktop.
#! /bin/zsh
usage () {
echo "usage: ${0:t} [-f known_hosts] host..."
}
KNOWN_HOSTS="$HOME/.ssh/known_hosts"
expanded_hosts() {
case $(uname) in
Darwin)
echo -e "$1\t$(host $1 | awk '/address/ {print $4 "\t" $1}')"
;;
*)
echo -e "$1\t$(getent hosts $1)"
;;
esac
}
while getopts ':hf:' opt; do
case "$opt" in
f)
$KNOWN_HOSTS="$OPTARG"
;;
h|?)
usage
exit
;;
esac
done
if [[ $# -lt 1 ]]; then
usage
exit
fi
for arg in $*; do
hosts="$(expanded_hosts $arg)"
for h in ${=hosts}; do
ssh-keygen -f "$KNOWN_HOSTS" -R $h
done
ssh-keyscan -tdsa,rsa ${=hosts} >>"$KNOWN_HOSTS" 2>/dev/null
done
@slumos
Copy link
Author

slumos commented Feb 7, 2012

I use it with zsh parameter generation like:

 $ update-known-hosts.sh vdb-{001..016}

But you could do this on BSD:

$ update-known-hosts.sh $(jot -w vdb-%03d 16)

Or this on Linux: :-(

$ update-known-hosts.sh $(printf 'vdb-%03d\n' $(seq 16))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment