1 Commits

Author SHA1 Message Date
matmoul 02bc8eb725 docs: rewrite README usage and examples 2026-04-27 22:49:10 +02:00
+28 -97
View File
@@ -1,111 +1,42 @@
#!/bin/bash # sshrm
set -euo pipefail Remove an entry from `~/.ssh/known_hosts` by host name or by line number.
readonly FILENAME="${HOME}/.ssh/known_hosts" ## Usage
readonly BACKUP_FILE="${FILENAME}.sshrm.bak"
check_known_hosts() { ```bash
if [ ! -f "${FILENAME}" ]; then sshrm [-h|--help] [--no-backup] host|line_number
error "known_hosts not found." ```
exit 1
fi
}
usage() { ## Options
echo "Usage: sshrm [-h|--help] [--no-backup] host|line_number"
}
confirm() { - `-h`, `--help` Show help and exit.
read -r -p "Proceed? [y/N] " answer - `--no-backup` Skip creating `~/.ssh/known_hosts.sshrm.bak`.
case "$answer" in
y|Y|yes|YES)
return 0
;;
*)
error "cancelled."
return 1
;;
esac
}
error() { ## Examples
echo "Error: $1"
}
is_number() { Remove by host:
case "$1" in
''|*[!0-9]*) return 1 ;;
*) return 0 ;;
esac
}
get_host_from_line() { ```bash
awk -v line="$1" 'NR == line {print $1; exit}' "${FILENAME}" sshrm example.com
} ```
create_backup() { Remove by line number:
cp "${FILENAME}" "${BACKUP_FILE}"
}
remove_host() { ```bash
ssh-keygen -R "$1" sshrm 12
} ```
if [ "${1-}" = "" ]; then Remove without backup:
error "missing argument."
usage
exit 1
fi
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then ```bash
usage sshrm --no-backup example.com
exit 0 ```
fi
backup=true ## Notes
if [ "$1" = "--no-backup" ]; then
backup=false
shift
fi
if [ "${1-}" = "" ]; then - Line numbers must be positive integers.
error "missing argument." - Extra arguments are rejected.
usage - The script asks for confirmation before removal.
exit 1 - The script edits `~/.ssh/known_hosts`.
fi - Keep a backup if you need to recover entries.
if [ "${2-}" != "" ]; then
error "too many arguments."
usage
exit 1
fi
check_known_hosts
if is_number "$1"; then
if [ "$1" -eq 0 ]; then
error "invalid line number."
exit 1
fi
HOST="$(get_host_from_line "$1")"
if [ "${HOST}" = "" ]; then
error "invalid line number."
exit 1
fi
else
HOST="$1"
fi
if ! confirm; then
exit 1
fi
if [ "${backup}" = true ]; then
create_backup
fi
if ! remove_host "$HOST"; then
error "removal failed."
exit 1
fi