diff --git a/local/bin/sshconnect.sh b/local/bin/sshconnect.sh index 2cd70af..471aadc 100755 --- a/local/bin/sshconnect.sh +++ b/local/bin/sshconnect.sh @@ -1,50 +1,60 @@ #!/bin/bash -# 1. Liste des hosts -mapfile -t ALL_HOSTS < <(grep -riI "^Host " ~/.ssh/config ~/.ssh/config.d/ ~/.ssh/include/ 2>/dev/null | awk '{print $2}' | grep -v '*' | sort -u) +mapfile -t ALL_HOSTS < <( + grep -hriI "^Host " ~/.ssh/config ~/.ssh/config.d/ ~/.ssh/include/ 2>/dev/null \ + | awk '{print $2}' \ + | grep -v '\*' \ + | sort -u +) + +TOTAL=${#ALL_HOSTS[@]} check_host() { local host=$1 - local info=$(ssh -G "$host") - local addr=$(echo "$info" | awk '/^hostname / {print $2}') - local port=$(echo "$info" | awk '/^port / {print $2}') + local info addr port user + info=$(ssh -G "$host" 2>/dev/null) + addr=$(awk '/^hostname / {print $2}' <<< "$info") + port=$(awk '/^port / {print $2}' <<< "$info") + user=$(awk '/^user / {print $2}' <<< "$info") - # Test de port TCP if (timeout 0.7s bash -c "cat < /dev/null > /dev/tcp/$addr/$port") 2>/dev/null; then - # On utilise "|" comme délimiteur interne - printf "[ ON ]|%s\n" "$host" + printf "\033[32m[ ON ]\033[0m|ON|%-22s|%s@%s\n" "$host" "$user" "$addr" else - printf "[ OFF ]|%s\n" "$host" + printf "\033[31m[ OFF ]\033[0m|OFF|%-22s|%s@%s\n" "$host" "$user" "$addr" fi } export -f check_host -echo "Vérification des serveurs..." -# 2. Scan parallèle -STATE_LIST=$(printf "%s\n" "${ALL_HOSTS[@]}" | xargs -I {} -P 10 bash -c 'check_host "{}"') +printf "Vérification de %d serveurs...\n" "$TOTAL" -# 3. Interface FZF -# On demande à fzf d'afficher les colonnes proprement -choice=$(echo "$STATE_LIST" | fzf --height 40% --reverse \ - --delimiter="\|" \ - --with-nth=1,2 \ - --header "Tapez 'OFF' pour les serveurs HS | 'ON' pour les actifs") +# Scan parallèle — ON affiché en premier, puis tri alphabétique +STATE_LIST=$( + printf "%s\n" "${ALL_HOSTS[@]}" \ + | xargs -I {} -P 20 bash -c 'check_host "{}"' \ + | sort -t'|' -k2,2r -k3,3 +) + +choice=$(echo "$STATE_LIST" | fzf \ + --ansi \ + --height 70% \ + --reverse \ + --delimiter="|" \ + --with-nth=1,3,4 \ + --header "Entrée: connexion | ESC: annuler | Filtre: ON / OFF / nom" \ + --preview 'host=$(echo {} | cut -d"|" -f3 | tr -d " "); ssh -G "$host" 2>/dev/null | grep -E "^(hostname|user|port|identityfile|compression) " | column -t' \ + --preview-window=right:45%:wrap) -# 4. Connexion propre if [ -n "$choice" ]; then - # On extrait le nom de l'hôte en utilisant le délimiteur "|" - host_to_connect=$(echo "$choice" | cut -d'|' -f2) - - # Nettoyage radical des caractères invisibles ou espaces restants - host_to_connect=$(echo "$host_to_connect" | tr -d '[:space:]') + status=$(echo "$choice" | cut -d'|' -f2) + host_to_connect=$(echo "$choice" | cut -d'|' -f3 | tr -d '[:space:]') - if [[ "$choice" == *"[ ON ]"* ]]; then + if [[ "$status" == "ON" ]]; then clear - ssh "$host_to_connect" + exec ssh "$host_to_connect" else - echo -e "\033[0;31m⚠️ Le serveur $host_to_connect semble OFFLINE.\033[0m" - read -p "Tenter quand même la connexion ? (y/n) " confirm - [[ $confirm == [yY] ]] && ssh "$host_to_connect" + printf "\033[0;31m⚠ Le serveur '%s' semble OFFLINE.\033[0m\n" "$host_to_connect" + read -rp "Tenter quand même la connexion ? (y/n) : " confirm + [[ $confirm == [yY] ]] && exec ssh "$host_to_connect" fi fi