docs: rewrite README usage and examples
This commit is contained in:
@@ -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"
|
||||
readonly BACKUP_FILE="${FILENAME}.sshrm.bak"
|
||||
## Usage
|
||||
|
||||
check_known_hosts() {
|
||||
if [ ! -f "${FILENAME}" ]; then
|
||||
error "known_hosts not found."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
```bash
|
||||
sshrm [-h|--help] [--no-backup] host|line_number
|
||||
```
|
||||
|
||||
usage() {
|
||||
echo "Usage: sshrm [-h|--help] [--no-backup] host|line_number"
|
||||
}
|
||||
## Options
|
||||
|
||||
confirm() {
|
||||
read -r -p "Proceed? [y/N] " answer
|
||||
case "$answer" in
|
||||
y|Y|yes|YES)
|
||||
return 0
|
||||
;;
|
||||
*)
|
||||
error "cancelled."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
- `-h`, `--help` Show help and exit.
|
||||
- `--no-backup` Skip creating `~/.ssh/known_hosts.sshrm.bak`.
|
||||
|
||||
error() {
|
||||
echo "Error: $1"
|
||||
}
|
||||
## Examples
|
||||
|
||||
is_number() {
|
||||
case "$1" in
|
||||
''|*[!0-9]*) return 1 ;;
|
||||
*) return 0 ;;
|
||||
esac
|
||||
}
|
||||
Remove by host:
|
||||
|
||||
get_host_from_line() {
|
||||
awk -v line="$1" 'NR == line {print $1; exit}' "${FILENAME}"
|
||||
}
|
||||
```bash
|
||||
sshrm example.com
|
||||
```
|
||||
|
||||
create_backup() {
|
||||
cp "${FILENAME}" "${BACKUP_FILE}"
|
||||
}
|
||||
Remove by line number:
|
||||
|
||||
remove_host() {
|
||||
ssh-keygen -R "$1"
|
||||
}
|
||||
```bash
|
||||
sshrm 12
|
||||
```
|
||||
|
||||
if [ "${1-}" = "" ]; then
|
||||
error "missing argument."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
Remove without backup:
|
||||
|
||||
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
|
||||
usage
|
||||
exit 0
|
||||
fi
|
||||
```bash
|
||||
sshrm --no-backup example.com
|
||||
```
|
||||
|
||||
backup=true
|
||||
if [ "$1" = "--no-backup" ]; then
|
||||
backup=false
|
||||
shift
|
||||
fi
|
||||
## Notes
|
||||
|
||||
if [ "${1-}" = "" ]; then
|
||||
error "missing argument."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
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
|
||||
- Line numbers must be positive integers.
|
||||
- Extra arguments are rejected.
|
||||
- The script asks for confirmation before removal.
|
||||
- The script edits `~/.ssh/known_hosts`.
|
||||
- Keep a backup if you need to recover entries.
|
||||
|
||||
Reference in New Issue
Block a user