2

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!

masotann
  • 121

2 Answers2

2

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
   hostname 1.2.3.4
   User root
   LocalForward 5951:192.168.1.1:5901

   ProxyCommand ssh 1.2.3.4 nc 192.168.1.1 22

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

Eric Renouf
  • 18,431
0

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
    1)
      serv1
    ;;
    2)
      serv2
    ;;

    *)
      echo "Invalid choice"
    esac
}

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"
fi