Modification du fonctionnement de la connexion des utilisateurs. Désormais la gestion des clés SSH passe pas l'agent SSH et non plus par des mouvements de fichiers pour plus de sécurité et moins d'écritures sur le disque.
56 lines
2.2 KiB
Bash
56 lines
2.2 KiB
Bash
#!/bin/bash
|
|
|
|
AUTHORIZED_SERVERS_PATH="opt/public/servers"
|
|
AUTHORIZED_SERVERS_FILE="/$AUTHORIZED_SERVERS_PATH/authorized_servers.txt"
|
|
CONNECTED_USER="$(whoami)"
|
|
USER_SERVERS=$(grep -w "$CONNECTED_USER" $AUTHORIZED_SERVERS_FILE)
|
|
|
|
if [ -z "$USER_SERVERS" ]; then
|
|
echo "Vous n'avez pas l'autorisation de vous connecter à un serveur."
|
|
exit 1
|
|
fi
|
|
|
|
function main_menu () {
|
|
echo "Veuillez sélectionner un serveur auquel vous connecter :"
|
|
|
|
counter=1
|
|
declare -A server_map
|
|
while read -r line; do
|
|
ip=$(echo "$line" | cut -d ' ' -f 1)
|
|
port=$(echo "$line" | cut -d ' ' -f 2)
|
|
custom_name=$(echo "$line" | cut -d ' ' -f 3)
|
|
server_user=$(echo "$line" | cut -d ' ' -f 4)
|
|
server_authkey=$(echo "$line" | cut -d ' ' -f 6)
|
|
server_map[$counter]="$ip $port $server_user $server_authkey"
|
|
echo "$counter) $custom_name - $server_user $ip:$port"
|
|
counter=$((counter + 1))
|
|
done <<< "$USER_SERVERS"
|
|
|
|
echo "$counter) Tapez 'quit' ou $counter pour vous déconnecter."
|
|
|
|
read -r -p "Votre choix (1-$counter): " choice
|
|
|
|
if [ "$choice" == "quit" ] || [ "$choice" == "$counter" ]; then
|
|
echo "Déconnexion du bastion."
|
|
exit 0
|
|
elif [ -z "$choice" ] || [ -z "${server_map[$choice]}" ]; then
|
|
echo "Sélection invalide."
|
|
else
|
|
selected_server="${server_map[$choice]}"
|
|
echo "Connexion à $selected_server..."
|
|
if [ -z "$(echo "$selected_server" | cut -d ' ' -f 4)" ]; then
|
|
ttyrec -z --"$(echo "$selected_server" | cut -d ' ' -f 1)"-"$(echo "$selected_server" | cut -d ' ' -f 3)"-- -k 300 --warn-before-kill 60 -- ssh -p "$(echo "$selected_server" | cut -d ' ' -f 2)" "$(echo "$selected_server" | cut -d ' ' -f 3)"@"$(echo "$selected_server" | cut -d ' ' -f 1)"
|
|
else
|
|
eval "$(ssh-agent)"
|
|
trap 'kill $SSH_AGENT_PID' EXIT
|
|
cat /"$AUTHORIZED_SERVERS_PATH"/"$(echo "$selected_server" | cut -d ' ' -f 4)" | ssh-add -
|
|
ttyrec -z --"$(echo "$selected_server" | cut -d ' ' -f 1)"-"$(echo "$selected_server" | cut -d ' ' -f 3)"-- -k 300 --warn-before-kill 60 -- ssh -p "$(echo "$selected_server" | cut -d ' ' -f 2)" "$(echo "$selected_server" | cut -d ' ' -f 3)"@"$(echo "$selected_server" | cut -d ' ' -f 1)"
|
|
ssh-add -D
|
|
fi
|
|
fi
|
|
}
|
|
|
|
while true; do
|
|
main_menu
|
|
done
|