Files
scripts-bash/local/bin/sshconnect.sh

51 lines
1.6 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 1. Liste des hosts
mapfile -t ALL_HOSTS < <(grep -riI "^Host " ~/.ssh/config ~/.ssh/config.d/ 2>/dev/null | awk '{print $2}' | grep -v '*' | sort -u)
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}')
# 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"
else
printf "[ OFF ]|%s\n" "$host"
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 "{}"')
# 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")
# 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:]')
if [[ "$choice" == *"[ ON ]"* ]]; then
clear
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"
fi
fi