73 lines
1.5 KiB
Bash
73 lines
1.5 KiB
Bash
#!/usr/bin/env bash
|
|
#
|
|
# Open an interactive menu and connect to a selected server through SSH.
|
|
#
|
|
# This is useful for small operations teams that frequently access a known
|
|
# list of Linux servers. Edit the SERVERS array to match your environment.
|
|
#
|
|
# Usage:
|
|
# bash ssh_server_menu.sh
|
|
#
|
|
# Server format:
|
|
# "Display Name|ssh_user|hostname_or_ip|port"
|
|
|
|
set -Eeuo pipefail
|
|
|
|
SERVERS=(
|
|
"Production Web 01|deploy|192.0.2.10|22"
|
|
"Production Database 01|postgres|192.0.2.20|22"
|
|
"Monitoring Server|admin|192.0.2.30|22"
|
|
"Backup Server|backup|192.0.2.40|22"
|
|
)
|
|
|
|
print_menu() {
|
|
clear
|
|
printf 'SSH Server Menu\n'
|
|
printf '===============\n\n'
|
|
|
|
local index=1
|
|
local item name
|
|
for item in "${SERVERS[@]}"; do
|
|
IFS='|' read -r name _ _ _ <<< "$item"
|
|
printf '%s) %s\n' "$index" "$name"
|
|
index=$((index + 1))
|
|
done
|
|
|
|
printf 'q) Quit\n\n'
|
|
}
|
|
|
|
connect_to_server() {
|
|
local selected="$1"
|
|
local name user host port
|
|
|
|
IFS='|' read -r name user host port <<< "$selected"
|
|
|
|
printf 'Connecting to %s (%s@%s:%s)...\n' "$name" "$user" "$host" "$port"
|
|
ssh -p "$port" "${user}@${host}"
|
|
}
|
|
|
|
while true; do
|
|
print_menu
|
|
read -rp 'Choose a server: ' choice
|
|
|
|
case "$choice" in
|
|
q|Q)
|
|
printf 'Bye.\n'
|
|
exit 0
|
|
;;
|
|
''|*[!0-9]*)
|
|
printf 'Invalid option. Press Enter to try again.'
|
|
read -r _
|
|
;;
|
|
*)
|
|
if (( choice >= 1 && choice <= ${#SERVERS[@]} )); then
|
|
connect_to_server "${SERVERS[$((choice - 1))]}"
|
|
else
|
|
printf 'Invalid option. Press Enter to try again.'
|
|
read -r _
|
|
fi
|
|
;;
|
|
esac
|
|
done
|
|
|