Skip to content

Instantly share code, notes, and snippets.

@driversti
Last active January 24, 2025 07:29
Show Gist options
  • Select an option

  • Save driversti/616e16176367e932b19692d4b3879ac5 to your computer and use it in GitHub Desktop.

Select an option

Save driversti/616e16176367e932b19692d4b3879ac5 to your computer and use it in GitHub Desktop.
SSH to servers with handy navigation
#!/bin/bash
################################################
### GENERATED BY AI (tested on macOS) ###
################################################
# Hide cursor and show it again on exit
trap 'printf "\033[?25h"' EXIT
printf '\033[?25l'
# Define your servers here (Name username ip port)
servers=(
"Alpha ubuntu 192.168.20.11 22"
"Bravo ubuntu 192.168.20.12 22"
"Charlie ubuntu 192.168.20.13 22"
"Delta ubuntu 192.168.20.14 22"
"Echo ubuntu 192.168.20.15 22"
"Foxtrot ubuntu 192.168.20.16 22"
"Golf ubuntu 192.168.20.17 22"
)
selected=0
server_count=${#servers[@]}
display_menu() {
clear
printf "\033[1;36mSelect server:\033[0m\n"
for i in "${!servers[@]}"; do
if [ $i -eq $selected ]; then
printf "\033[44m\033[37m > ${servers[$i]}\033[0m\n"
else
printf " ${servers[$i]}\n"
fi
done
printf "\n\033[1;33m/ to navigate | Enter to connect | Ctrl+C to exit\033[0m"
}
display_menu
while true; do
read -rsn1 key
case "$key" in
$'\x1B') # Escape sequence
read -rsn2 key
case "$key" in
'[A') # Up arrow
((selected > 0)) && ((selected--))
display_menu
;;
'[B') # Down arrow
((selected < server_count - 1)) && ((selected++))
display_menu
;;
esac
;;
'') # Enter key
break
;;
$'\x03') # Ctrl+C
exit
;;
esac
done
# Split selected server into components
IFS=' ' read -r name username ip port <<< "${servers[$selected]}"
# Start SSH connection
clear
printf "\033[?25h" # Restore cursor before SSH
echo "Connecting to $name ($ip) ..."
ssh -p "$port" "$username@$ip"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment