For people who manage remote servers, the use of the secure shell (ssh) Terminal command is quite common, and is very often the only command people use when opening a Terminal window. While you can enter each of your ssh connections (usernames, server, and arguments) for every new Terminal session, this can be a burden if you need to connect to many servers. While Apple's Terminal application has a connection manager option where you can store the addresses of the servers you access, even this can be a bit cumbersome to continually access. One option instead is to implement a start-up script for the Terminal that will present you with a quick menu of your servers.
For people who are Terminal savvy this should be a fairly straightforward task. If you are not too familiar with the Terminal or do not regularly use ssh, then you might consider avoiding this tip as at the very least it would be unnecessary for you.
The first thing you will need to do is create the script, which can be done by using any terminal editor. I enjoy pico/nano, so to create the script with these editors you would do the following:
1. Enter "pico connect.sh" in the Terminal, where "connect.sh" is the name of the script (you can call it anything you like).
2. Copy the following script to the Terminal window:
#!/bin/bash PS3='Pick a server (or ctrl-c to quit): ' select item in \ "firstname.lastname@example.org" \ "email@example.com" \ "firstname.lastname@example.org" do clear echo You are connecting to $item PS3='Choose connection type (or ctrl-c to quit): ' select type in "ssh" "sftp" "ftp" "rsh" "telnet" "rlogin" "quit" do echo Enter additional $type arguments \(or just press enter for none\): read args case "$type" in ssh ) ssh $args $item ;; sftp ) sftp $args $item ;; ftp ) ftp $args $item ;; rsh ) rsh $args $item ;; telnet ) telnet $args $item ;; rlogin ) rlogin $args $item ;; quit ) break ;; "") please select one of the above or press ctrl-c ;; esac exit 0 done exit 0 done
3. Edit the script so your various servers are listed with the appropriate username and passwords (replacing the "...server1.com," "...server2.com," and "server3.com" entries here--see security tips below). Ensure when you add new servers that you add the single space followed by the back slash after their names in quotes, if there is another server listing beneath it. These slashes will "escape" the return character after them, so all the servers listed will be part of the same "select item in..." command (doing this just makes the list of servers easier to manage in the script, instead of having them all in one long line).
4. Press Control-O, followed by Enter, and then Control-X to save and quit the editor (the script should then be saved to your home folder).
5. Next ensure the script is executable by running the following command to enable its execute bit (substituting "connect.sh" with the name of your script):
chmod u+x connect.sh
6. With the script saved and executable, move it to a desired location on your hard drive (such as your Documents folder). You can even put a period before its name (i.e., ".connect.sh") to leave it where it is and make it invisible in the Finder.
With the script saved you can run it directly from within the Terminal by using the dot-slash notation "./path/to/connect.sh" as you would run any shell script; however, to make it run each time you create a new Terminal window you would need to add it to your shell's start-up file. To do this for the default "bash" shell in OS X, you will need to add the script to the hidden ".bashrc" file that is in your home directory.
1. Enter "pico .bashrc" in the Terminal to create and edit the .bashrc file if it does not already exist.
2. Enter the command to execute the script in the editor so it points to the place where you have saved your script. For example, if you have it in your Documents folder then you would type the following:
3. Save the .bashrc file and exit the editor by using the Control-O and Control-X hot keys.
After the .bashrc file has been saved then you should be all set. Now whenever you open the Terminal it will prompt you to select one of your servers. After this you can select the type of connection (ssh, sftp, telnet, etc.) and provide any additional arguments if needed, and then be off and running. To continue a normal Terminal session without using this menu, you can press control-C at any time to cancel it and return to the command prompt.
Security tips: While this script will work conveniently, storing your passwords in it is just like having them in any unencrypted text file and this does pose a security risk. Alternative options for doing this are to place the script in an encrypted disk image that you have set to mount at login, and removing the "|password" portion of each server entry. The disk image option will keep the file encrypted, and removing the password from it will just require you to enter your password for each connection (this is a preferred setup). A final option you can do is set up ssh keys so no password will be required and your ssh server will automatically recognize your system.
While this script is for ssh and similar remote connections, you can use its syntax to set up other common commands that you regularly use. The idea here is to add a neat menu option instead of having to access cumbersome GUI elements or enter your connection sessions manually. Do you have any tips for managing common tasks in the Terminal? If so then let us know in the comments.