Skip to content

Instantly share code, notes, and snippets.

@ryot4
Last active October 13, 2022 16:00
Show Gist options
  • Save ryot4/6a462fa247312c786ffcf5aebd3a2cea to your computer and use it in GitHub Desktop.
Save ryot4/6a462fa247312c786ffcf5aebd3a2cea to your computer and use it in GitHub Desktop.
A simple wrapper for ssh-keygen command to manage known_hosts file
_ssh_known_hosts()
{
local cur prev
_get_comp_words_by_ref cur prev
COMPREPLY=($(compgen -W 'find forget list' -- "${cur}"))
}
complete -F _ssh_known_hosts ssh-known-hosts
#!/bin/sh
usage()
{
echo "$(basename "$0") [-f known_hosts_file] {find|forget|list} {hostname|[hostname]:port}"
}
known_hosts_file="${HOME}/.ssh/known_hosts"
while getopts f:h option; do
case "${option}" in
f)
known_hosts_file="${OPTARG}"
;;
h)
usage
exit
;;
esac
done
shift $((OPTIND - 1))
if [ $# -eq 0 ]; then
echo 'no command specified' 1>&2
exit 1
fi
case "$1" in
find)
if [ -z "$2" ]; then
echo 'no hostname specified' 1>&2
exit 1
fi
ssh-keygen -f "${known_hosts_file}" -F "$2"
;;
forget)
if [ -z "$2" ]; then
echo 'no hostname specified' 1>&2
exit 1
fi
ssh-keygen -f "${known_hosts_file}" -R "$2"
;;
list)
cat "${known_hosts_file}"
;;
*)
echo "unknown command: $1" 1>&2
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment