I work in operations and need to be able to log into multiple servers among a few hundred servers in a productive way. What I mean by productive is this: from the time from when someone asks me to look at a series of hosts to the time I actually get in to the hosts should be minimal. Right now I have to go look at my notes, find the host names, add my username in front, put the series of hosts in a text editor and paste that command into the terminal, which gets very tedious.

Ideally I can make some function I can call from the termial that'll help me filter through my host list and find the ones I need, add my username and create my ssh(or csshx) command. I've thought about having the following text file:

description           hostname          user
.........             .........         ...

and using cat to find the hosts I need, and concatenate the user@ to the front and creating the command. I feel that there is a better way out there and I'd like to see people's opinion on this.

Thanks for your time!

  • What OS is your desktop running? – Just a learner Apr 22 '15 at 23:18
  • i'm running mac os x! – masotann Apr 24 '15 at 16:53

You could, though it may become unwieldy, add entries to your ~/.ssh/config for each descriptive name you want, then you can just ssh <name> and you can have the correct user/host etc. automatically applied. Here's an example from my config

host abc
   User root
   LocalForward 5951:

   ProxyCommand ssh nc 22

So I can just type ssh abc and it will log me in as root on and setup some port forwarding and tunnel my ssh to another host further on

  • You can also use "Host *" with "User" to define a default username for all hosts. – wurtel Apr 23 '15 at 7:32

I use aliases to be able to type a small command to go to different servers. So instead of having to type a long command I just type the alias. Now as you start adding more and more servers, it becomes harder to remember that server1 has an alias of 'one' and server97 has an alias of 'fred' so the first alias idea only works for the servers I access most often.

For others I create a function (which is also aliased) so that when I call it I am presented with a list of servers to connect to. Based on the number I enter, it runs the alias to connect to that server.

I keep my aliases in a .bash_aliases file (naturally, this is OS-dependent on where/how you define), and my functions in a .bash_functions file that I then load from .bashrc. This allows for easy maintenance instead of having a huge bashrc file.

So, for example, you could have the following entries in your alias file:

alias serv1='ssh username@server1.domain.com'<br>
alias serv2='ssh username@server2.domain.com'<br>
alias start='serverconnect'

And then have a function like this:

function serverconnect
    echo "Select server you would like to connect to:"
    echo " 1 - Server1"
    echo " 2 - Server2"

    read choice

    case $choice in

      echo "Invalid choice"

So if I need to connect to server1 I would just type 'serv1', whereas if I wanted a list of servers to connect to I type 'start'. It can become a challenge as the list of servers grows if you want to display them all in a single list (which would likely scroll off the screen) or break them up further. It really all depends on what works for your process flow.

Update: Thought you might also need the way I call the aliases and function file. In .bashrc I use the following syntax:

if [ -f "${HOME}/.bash_aliases" ]; then
  source "${HOME}/.bash_aliases"

Not the answer you're looking for? Browse other questions tagged or ask your own question.