Improved the SSH error handling.

This commit is contained in:
Raphaël Numbus
2026-05-29 16:53:24 +02:00
parent 39104ecf92
commit f6063fe153
2 changed files with 32 additions and 4 deletions
+30 -4
View File
@@ -118,11 +118,18 @@ setup_ssh() {
echod "\n ➡️ Copying SSH key to target host '${TARGET_USER}@${LIVE_TARGET_IP}'..."
while true; do
if sshpass -p "${LIVE_TARGET_PASSWORD}" ssh-copy-id -o StrictHostKeyChecking=no -i "${TMP_EXTRA_PATH}/home/numbus-admin/.ssh/id_ed25519" "${TARGET_USER}@${LIVE_TARGET_IP}" >> "${STDOUT}" 2>> "${STDERR}"; then
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
else
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\"").
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
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 :
- You ran this script multiple times
@@ -133,8 +140,27 @@ 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; }
sed -i "/${LIVE_TARGET_IP}/d" "/home/${USER}/.ssh/known_hosts"
gum confirm "Remove the old fingerprint and accept the new one ?" || { echo -e "\n\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
}
+2
View File
@@ -1,3 +1,5 @@
0: successful.
1: error.
225: Bad SSH credentials.
226: Host unreachable or connection refused.
22: SSH fingerprint in `known_hosts` for the IP is different than the current one.