5 Commits

3 changed files with 71 additions and 11 deletions
+1 -8
View File
@@ -11,15 +11,8 @@ 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. - 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. - 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` 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, reject extra arguments, and may support a dedicated `--no-backup` option with a `known_hosts.sshrm.bak` backup file.
# Project identity # Project identity
- Main script: `sshrm` - Main script: `sshrm`
- License: GNU GPL v3 - License: GNU GPL v3
git checkout "${TAGBRANCH}"
git merge "${CURRENTBRANCH}"
git push
git tag -a "${VERSION}" -m "${MESSAGE}"
git push --tags
git checkout "${CURRENTBRANCH}"
+33
View File
@@ -1,2 +1,35 @@
# sshrm # sshrm
Remove an entry from `~/.ssh/known_hosts` by host name or by line number.
## Usage
```bash
sshrm [OPTION]... HOST|LINE
```
## Options
- `-h`, `--help` Show help and exit.
## Examples
Remove by host:
```bash
sshrm example.com
```
Remove by line number:
```bash
sshrm 12
```
## Notes
- Line numbers must be positive integers.
- Extra arguments are rejected.
- The script edits `~/.ssh/known_hosts`.
- Keep a backup if you need to recover entries.
+37 -3
View File
@@ -1,8 +1,9 @@
#!/bin/bash #!/bin/bash
set -u set -euo pipefail
readonly FILENAME="${HOME}/.ssh/known_hosts" readonly FILENAME="${HOME}/.ssh/known_hosts"
readonly BACKUP_FILE="${FILENAME}.sshrm.bak"
check_known_hosts() { check_known_hosts() {
if [ ! -f "${FILENAME}" ]; then if [ ! -f "${FILENAME}" ]; then
@@ -12,7 +13,7 @@ check_known_hosts() {
} }
usage() { usage() {
echo "Usage: sshrm [-h|--help] host|line_number" echo "Usage: sshrm [-h|--help] [--no-backup] host|line_number"
} }
error() { error() {
@@ -30,6 +31,10 @@ get_host_from_line() {
awk -v line="$1" 'NR == line {print $1; exit}' "${FILENAME}" awk -v line="$1" 'NR == line {print $1; exit}' "${FILENAME}"
} }
create_backup() {
cp "${FILENAME}" "${BACKUP_FILE}"
}
remove_host() { remove_host() {
ssh-keygen -R "$1" ssh-keygen -R "$1"
} }
@@ -45,9 +50,31 @@ if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
exit 0 exit 0
fi fi
backup=true
if [ "$1" = "--no-backup" ]; then
backup=false
shift
fi
if [ "${1-}" = "" ]; then
error "missing argument."
usage
exit 1
fi
if [ "${2-}" != "" ]; then
error "too many arguments."
usage
exit 1
fi
check_known_hosts check_known_hosts
if is_number "$1"; then if is_number "$1"; then
if [ "$1" -eq 0 ]; then
error "invalid line number."
exit 1
fi
HOST="$(get_host_from_line "$1")" HOST="$(get_host_from_line "$1")"
if [ "${HOST}" = "" ]; then if [ "${HOST}" = "" ]; then
error "invalid line number." error "invalid line number."
@@ -57,4 +84,11 @@ else
HOST="$1" HOST="$1"
fi fi
remove_host "$HOST" if [ "${backup}" = true ]; then
create_backup
fi
if ! remove_host "$HOST"; then
error "removal failed."
exit 1
fi