first commit

This commit is contained in:
2026-10-01 10:42:36 +03:00
commit d242955f4e
4 changed files with 431 additions and 0 deletions
+78
View File
@@ -0,0 +1,78 @@
#!/usr/bin/env bash
#
# generate_ssh_keys.sh - Create SSH key pairs for the QNAP devices listed in
# nas_list.conf that use method "ssh", and optionally install the public key
# on each QNAP for passwordless auth.
#
# Run this ON TrueNAS, as the same user that will run migrate.sh (root).
#
# Usage:
# ./generate_ssh_keys.sh [--config FILE] [--only NAME] [--copy] [-h|--help]
#
# --config FILE NAS list to read (default ./nas_list.conf)
# --only NAME Only process one NAS entry by name
# --copy Also run ssh-copy-id to install the key on the QNAP
# (prompts once for the QNAP admin password per device)
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="${SCRIPT_DIR}/nas_list.conf"
ONLY_NAME=""
COPY_KEY=0
usage() {
grep '^#' "$0" | sed -n '2,15p' | sed 's/^# \{0,1\}//'
exit 1
}
log() { printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"; }
die() { log "ERROR: $*" >&2; exit 1; }
while [ $# -gt 0 ]; do
case "$1" in
--config) CONFIG_FILE="$2"; shift 2 ;;
--only) ONLY_NAME="$2"; shift 2 ;;
--copy) COPY_KEY=1; shift ;;
-h|--help) usage ;;
*) die "unknown argument: $1" ;;
esac
done
[ -f "$CONFIG_FILE" ] || die "config file not found: $CONFIG_FILE (copy nas_list.conf.example)"
command -v ssh-keygen >/dev/null || die "ssh-keygen not found"
while IFS= read -r line || [ -n "$line" ]; do
line="${line%%#*}"
[ -z "${line// /}" ] && continue
read -r name method host user auth shares <<< "$line"
[ -n "$name" ] || continue
[ "$method" = "ssh" ] || continue
[ -n "$ONLY_NAME" ] && [ "$ONLY_NAME" != "$name" ] && continue
if [ "$auth" = "-" ]; then
log "[$name] auth is '-' in config (use ssh-agent/default key), skipping generation"
continue
fi
mkdir -p "$(dirname "$auth")"
chmod 700 "$(dirname "$auth")"
if [ -f "$auth" ]; then
log "[$name] key already exists at $auth, skipping generation"
else
log "[$name] generating ed25519 key at $auth"
ssh-keygen -t ed25519 -f "$auth" -N "" -C "truenas-migration-${name}"
chmod 600 "$auth"
chmod 644 "${auth}.pub"
fi
if [ "$COPY_KEY" -eq 1 ]; then
command -v ssh-copy-id >/dev/null || die "ssh-copy-id not found, install openssh-client"
log "[$name] installing public key on ${user}@${host} (password prompt follows)"
ssh-copy-id -i "${auth}.pub" -o StrictHostKeyChecking=accept-new "${user}@${host}" \
|| log "[$name] ssh-copy-id failed, add ${auth}.pub to the QNAP manually"
fi
done < "$CONFIG_FILE"
log "Done. Test access with: ssh -i <key> <user>@<host>"