Multi-server login functions

As my first post on this site, I figured it would be a great idea to make it about the most basic feature of multi-system administration: Multi-server authentication.

Now, I've written a couple functions to handle this.


function goto(){  
        case "$1" in
                @(server1|server2|server3|server4|server5) )
                        echo "LDAP login"
                        ssh -i /home/$my_user/.ssh/id_rsa 'LDAP_DOMAIN\'$my_user@$1
                        echo "Non-LDAP login"
                        ssh -i /home/$my_user/.ssh/id_rsa $my_user@$1

As you can see in the function above, it allows for providing a list of LDAP servers. You can just as easily tell it to use a different private key based on the server name. Case statements are great for that sort of thing.

Now, functions are kind of useless without a tab completion, so I created that as well. First, you'll need a text file with all servers in it (one line, a single space between names):


server1 server2 server3 server4 server5 server6 server7 server8 server9 server10 server11  

Once you have that in place, add this to your ~/.bashrc as well:


        local COMPLETES=`cat /home/$my_user/server_list/servers.txt`
        COMPREPLY=( $(compgen -W "$COMPLETES" -- ${COMP_WORDS[COMP_CWORD]}) )
        return 0
complete -F _goto_complete goto  

Adding these features will allow you to do things like this:

user@local:~$ goto server1  
LDAP login  
Welcome to Ubuntu 12.04.5 LTS (GNU/Linux 3.13.0-39-generic x86_64)

 * Documentation:

  System information as of Thu Mar  5 09:23:42 PST 2015

  System load:  0.11               Processes:           119
  Usage of /:   38.6% of 56.73GB   Users logged in:     0
  Memory usage: 25%                IP address for eth0:
  Swap usage:   1%

  Graph this data and manage this system at:

New release '14.04.1 LTS' available.  
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2017.

Last login: Mon Feb 23 07:55:01 2015 from  

Or for a non-LDAP server:

user@local:~$ goto server11  
Non-LDAP login  
Last login: Thu Mar  5 09:25:22 2015 from  
[user@server11 ~]$