Simple Bash Menu Script
When you have a wide array of internal servers that you need to be able to SSH into at any given moment to check this or that, I find it's best to spend a little time and craft a quick bash script.
This script allows you to present the user (yourself) with a menu of options and then select the one you want. Each menu option can run any number of different bash commands, but I find it helpful to just use shh
commands.
Create your script
First, create your script. I use the following structure which I pilfered from the hallowed pages of StackOverflow some time ago and regret not bookmarking it for attribution here.
#!/bin/bash
# Bash Menu Script Example
# Heading
echo; echo "SSH Menu"; echo
# Menu
PS3='Please enter your choice: '
options=("Server 1" "Server 2" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Server 1")
ssh server-1@example.com; break
;;
"Server 2")
ssh server-2@example.com; break
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done
Save this script as something obvious like "ssh_menu.sh
".
Make it easy to access
What is the use of having a handy menu if it's hard to find. To that end, I would recommend you create an alias. In most Linux flavors you can do this by editing your .bashrc
or .bash_profile
scripts. To keep things really simple I use the alias "s
" for my SSH menu.
Assuming you put your ssh_menu.sh
script in your home directory, you can add this to your .bashrc
or .bash_profile
script:
alias s="~/ssh_menu.sh"
Viola! Now you can access your menu of servers any time you need to by just typing the command "s
".
You may need to run source ~/.bashrc
or source ~/.bash_profile
to see the SSH menu like this:
$ s
SSH Menu
1) Server 1
2) Server 2
3) Quit
Please enter your choice: