-1

I want to store all servers in the local server(Linux Jumpbox), so I can connect by simply searching for specific hostname and ssh remotely from my Linux jump box.

So, the requirement is, input the server hostname, which should search in existing server list, if found, perform ssh connection, if not found, store under specific folder and perform ssh connection.

I tried the following simple if condition, but I actually want to categorize store servers under DEV, QA, UAT, PROD etc., using either under folder or LABEL.

Please share your suggestions/recommendations best way to manage the server list. Also suggest whether IF or CASE would be better approach for this.

***Script****

#!/bin/bash
if test -f /home/oracle/remote_hosts.lst
then
        echo "The File /home/oracle/remote_hosts.lst exists"
else
        echo "The File /home/oracle/remote_hosts.lst does not exist"
        exit 1
fi

echo "Please enter the Host Name of Remote Host System: \c"

read rhost

if grep $rhost /home/oracle/remote_hosts.lst 1> /dev/null 2>&1
then
        ssh -l oracle $rhost
else
        echo $rhost >> /home/oracle/remote_hosts.lst
        ssh -l oracle $rhost
fi

5
  • 1
    This is far too much work. What's wrong with just hitting the Tab key? Commented Feb 4, 2017 at 4:09
  • t#Tab is your friend as is grep -q
    – user9517
    Commented Feb 4, 2017 at 8:01
  • 1
    I'm voting to close this question as off-topic because we don't do code review.
    – user9517
    Commented Feb 4, 2017 at 8:02
  • if you have the bash-completion package installed, (on fedora f23 at least) it will tab complete any hosts in your ~/.ssh/known_hosts files, which saves a bunch of typing
    – Tom
    Commented Feb 4, 2017 at 18:08
  • Tom, Thanks for your inputs, the challenge is, we build lots of new servers in the cloud for old/new customers, so I will not find it most of time on existing list, but some of the operations on the old server helps to find an existing list. The label or folder option for each server type also would be helpful to differentiate what type of server it is. Commented Feb 7, 2017 at 2:58

1 Answer 1

1

Have you considered using the ssh config file?

Host web
    HostName webserver.server.com
    User www
Host db? maindb backupdb
    User oracle
Host github.com
    IdentityFile ~/.ssh/git.pub

You can use this to set default values for almost all command line flags, utilising wildcards, etc.

I personally combine this with simple bash aliases so say typing "sw" is an alias for "ssh web" just to make my life that little bit easier.

Just to clarify a few things above. You only need to override the values you need, so you can likely save time. If the only thing you need to override is the username, for example, you could put most the hosts under the same host, separated by spaces.

In my ssh config file, some definitions are 10 lines long, configured with port forwarding, and special keys, and x11, other definitions are only 1 line long, and matches thousands of hosts. In the example above, the "db?" would match "db3".

Using comments (#) you could still build the concept of labels (or just merge the same server types together).

If you want the server list for audit reasons, etc you can also use the 'LocalCommand' (and 'PermitLocalCommand') options to perform actions on the local machine, such as write out to a local text file.

1
  • Thanks for your valuable suggestions. Let me check ssh config file and see if that helps Commented Feb 7, 2017 at 2:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .