-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

Tom
  • 10,886
  • 5
  • 39
  • 62
CoolChap007
  • 13
  • 1
  • 3
  • 1
    This is far too much work. What's wrong with just hitting the Tab key? – Michael Hampton Feb 04 '17 at 04:09
  • t#Tab is your friend as is `grep -q` – user9517 Feb 04 '17 at 08:01
  • 1
    I'm voting to close this question as off-topic because we don't do code review. – user9517 Feb 04 '17 at 08: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 Feb 04 '17 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. – CoolChap007 Feb 07 '17 at 02:58

1 Answers1

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.

KHobbits
  • 1,090
  • 7
  • 13