From a23dd9dc2231f234d38b667c3917efd3df4ba6c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Numbus?= Date: Fri, 29 May 2026 20:11:58 +0200 Subject: [PATCH] Improved the SSH error handling. --- script/deploy.sh | 58 +++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/script/deploy.sh b/script/deploy.sh index d710c10..54e5cf4 100755 --- a/script/deploy.sh +++ b/script/deploy.sh @@ -110,6 +110,30 @@ hierarchy_preparation() { } setup_ssh() { + edit_var() { + EXIT_CODE=${1} + if [[ EXIT_CODE -eq 225 ]]; then + echo -e "\n ❌ Invalid password for ${TARGET_USER}@${LIVE_TARGET_IP}." + elif [[ EXIT_CODE -eq 226 ]]; then + echo -e "\n ❌ The IP address you specified cannot be reached." + fi + + echo -e " Please check the credentials provided in the configuration." + echo -e "\n Here are the current settings : + Target IP address : $(gum style --italic \"${LIVE_TARGET_IP}\") + Target password : $(gum style --italic \"${LIVE_TARGET_PASSWORD}\")" + gum confirm "Are these correct ?" || { + get_valid_input "LIVE_TARGET_IP" "\n ➡️ Provide the IP address of your machine in a NixOS live environment :" "192.168.1.100" "IP_REGEX"; + get_valid_input "LIVE_TARGET_IP" "\n ➡️ Provide the password of your machine in a NixOS live environment :" "password" "IP_REGEX" "true" "true"; + continue; + } + gum confirm "Retry connection ?" || { + echo -e "\n ❌ Host unreachable or connection refused."; + exit 226; + } + continue + } + echod "\n ➡️ Generating new SSH key for numbus-admin..." chmod 700 "${TMP_EXTRA_PATH}/home/numbus-admin/.ssh/" @@ -120,15 +144,14 @@ setup_ssh() { while true; do if sshpass -p "${LIVE_TARGET_PASSWORD}" ssh-copy-id -o StrictHostKeyChecking=no -o ConnectTimeout=10 -i "${TMP_EXTRA_PATH}/home/numbus-admin/.ssh/id_ed25519" "${TARGET_USER}@${LIVE_TARGET_IP}" >> "${STDOUT}" 2>> "${STDERR}"; then echod "\n ✅ SSH key copied successfully" - break + return 0 else local EXIT_CODE=$? if [[ ${EXIT_CODE} -eq 5 ]]; then - echo -e "\n ❌ Invalid password for ${TARGET_USER}@${LIVE_TARGET_IP}. Please check the credentials provided in the configuration." - exit 225 - fi - - if ssh-keygen -F "${LIVE_TARGET_IP}" >> "${STDOUT}" 2>> "${STDERR}"; then + edit_var "225" + elif ! ping -c 2 "${LIVE_TARGET_IP}" >> "${STDOUT}" 2>> "${STDERR}"; then + edit_var "226" + elif ssh-keygen -F "${LIVE_TARGET_IP}" >> "${STDOUT}" 2>> "${STDERR}"; then echo -e "\n ⚠️ The SSH fingerprint for the selected IP address $(gum style --italic "\"${LIVE_TARGET_IP}\"") is not the same as the one in $(gum style --italic "\".ssh/known_hosts\""). This could occur for multiple reasons : @@ -140,27 +163,12 @@ setup_ssh() { The script $(gum style --bold "cannot continue") without the correct fingerprint installed. If you are unsure, it is always better to check manually.\n" - gum confirm "Remove the old fingerprint and accept the new one ?" || { echo -e "\n\n ❌ SSH fingerprints don't match."; exit 22; } + gum confirm "Remove the old fingerprint and accept the new one ?" || { + echo -e "\n ❌ SSH fingerprints don't match."; + exit 22; + } ssh-keygen -R "${LIVE_TARGET_IP}" >> "${STDOUT}" 2>> "${STDERR}" fi - - while true; do - if ! ping -c 2 "${LIVE_TARGET_IP}" >> "${STDOUT}" 2>> "${STDERR}"; then - echo -e "\n ❌ The IP address you specified cannot be reached." - echo -e "\n Here are the current settings : - Target IP address : $(gum style --italic \"${LIVE_TARGET_IP}\") - Target password : $(gum style --italic \"${LIVE_TARGET_PASSWORD}\")" - gum confirm "Are these correct ?" || { - get_valid_input "LIVE_TARGET_IP" "\n ➡️ Provide the IP address of your machine in a NixOS live environment :" "192.168.1.100" "IP_REGEX"; - get_valid_input "LIVE_TARGET_IP" "\n ➡️ Provide the password of your machine in a NixOS live environment :" "password" "IP_REGEX" "true" "true"; - break; - } - gum confirm "Retry connection ?" || { echo -e "\n ❌ Host unreachable or connection refused."; exit 226; } - continue - else - break - fi - done fi done }