feat: add optional known_hosts backup control to sshrm
This commit is contained in:
@@ -11,7 +11,7 @@ description: SSHRM project conventions
|
||||
- Prefer minimal, focused changes that do not alter the intent of the existing scripts, unless the script behavior is intentionally updated.
|
||||
- Maintain `.continue/rules/project.md` whenever project conventions or script behavior change.
|
||||
- `sshrm` is implemented as a small Bash script with helper functions, while preserving host and line-number removal behavior.
|
||||
- `sshrm` should print a short usage line, support `-h`/`--help`, and fail clearly on missing or invalid line-number input.
|
||||
- `sshrm` should print a short usage line, support `-h`/`--help`, fail clearly on missing or invalid line-number input, and may support a dedicated `--no-backup` option with a `known_hosts.sshrm.bak` backup file.
|
||||
|
||||
# Project identity
|
||||
- Main script: `sshrm`
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
set -u
|
||||
|
||||
readonly FILENAME="${HOME}/.ssh/known_hosts"
|
||||
readonly BACKUP_FILE="${FILENAME}.sshrm.bak"
|
||||
|
||||
check_known_hosts() {
|
||||
if [ ! -f "${FILENAME}" ]; then
|
||||
@@ -12,7 +13,7 @@ check_known_hosts() {
|
||||
}
|
||||
|
||||
usage() {
|
||||
echo "Usage: sshrm [-h|--help] host|line_number"
|
||||
echo "Usage: sshrm [-h|--help] [--no-backup] host|line_number"
|
||||
}
|
||||
|
||||
error() {
|
||||
@@ -30,6 +31,10 @@ get_host_from_line() {
|
||||
awk -v line="$1" 'NR == line {print $1; exit}' "${FILENAME}"
|
||||
}
|
||||
|
||||
create_backup() {
|
||||
cp "${FILENAME}" "${BACKUP_FILE}"
|
||||
}
|
||||
|
||||
remove_host() {
|
||||
ssh-keygen -R "$1"
|
||||
}
|
||||
@@ -45,6 +50,18 @@ if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
backup=true
|
||||
if [ "$1" = "--no-backup" ]; then
|
||||
backup=false
|
||||
shift
|
||||
fi
|
||||
|
||||
if [ "${1-}" = "" ]; then
|
||||
error "missing argument."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
check_known_hosts
|
||||
|
||||
if is_number "$1"; then
|
||||
@@ -57,4 +74,8 @@ else
|
||||
HOST="$1"
|
||||
fi
|
||||
|
||||
if [ "${backup}" = true ]; then
|
||||
create_backup
|
||||
fi
|
||||
|
||||
remove_host "$HOST"
|
||||
|
||||
Reference in New Issue
Block a user