ia_dev/deploy/scripts/add-ssh-key-anthony.sh
2026-03-17 01:30:56 +01:00

46 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Add SSH public key for anthony@4nk to proxy and services (192.168.1.104).
# Run from ia_dev root. Uses DEPLOY_SSH_KEY or ~/.ssh/id_ed25519 to authenticate.
set -euo pipefail
SCRIPT_DIR="${BASH_SOURCE%/*}"
[[ -d "$SCRIPT_DIR" ]] || SCRIPT_DIR=.
LIB_DIR="$(cd "$SCRIPT_DIR/../_lib" && pwd)"
# shellcheck source=../_lib/ssh.sh
source "$LIB_DIR/ssh.sh"
SSH_KEY="${DEPLOY_SSH_KEY:-$HOME/.ssh/id_ed25519}"
SSH_USER="${DEPLOY_SSH_USER:-ncantu}"
PROXY_HOST="${DEPLOY_SSH_PROXY_HOST:-4nk.myftp.biz}"
SERVICES_IP="192.168.1.104"
KEY_LINE="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIM7df89eMnNBc85o+hijYTnZALlTZssIZSYlN+hMW3c anthony@4nk"
printf -v KEY_ESC '%q' "$KEY_LINE"
require_ssh_key "$SSH_KEY"
add_key_remote() {
local target_host="$1"
local use_proxy="${2:-false}"
local proxy_args=()
if [[ "$use_proxy" == "true" ]]; then
proxy_args=(-o "ProxyJump=$SSH_USER@$PROXY_HOST")
fi
# shellcheck disable=SC2207
local common_opts=($(ssh_common_opts "$SSH_USER" "$target_host"))
ssh -i "$SSH_KEY" \
"${common_opts[@]}" \
"${proxy_args[@]}" \
"$SSH_USER@$target_host" \
"mkdir -p ~/.ssh && (grep -qF $KEY_ESC ~/.ssh/authorized_keys 2>/dev/null || echo $KEY_ESC >> ~/.ssh/authorized_keys)"
}
echo "Adding key to proxy ($PROXY_HOST)..."
add_key_remote "$PROXY_HOST" false
echo "Adding key to services ($SERVICES_IP) via proxy..."
add_key_remote "$SERVICES_IP" true
echo "Done. Key anthony@4nk added on proxy and services."