Hi, everyone.

Today in this article I will show you how to create a simple automated ssh login session script based in a server list.

Prerequisites:

1 – sshpass (package): Available to be downloaded and installed by most of the Linux repositories.

2 – MobaXterm (Optional): https://mobaxterm.mobatek.net/download-home-edition.html

To be really pragmatic and democratic here, I will use the MobaXterm Standard Edition, which includes already the sshpass.

So, if you want to connect directly from your Windows machine, just install the MobaXterm and follow the same procedure below.

Let’s get started.

1 – Create a new file named by “serverlist” to store our desired servers (IP’s or hostnames):

vi serverlist

Contents:
<user>@<IP_or_Hostname> or <user>@<IP_or_Hostname> -p <port>

oracle@10.10.10.5
oracle@exadata.test.blog -p 2222
root@mydbserver

2 – Create a new file to store the password:

Content: <password>

e.g.:

vi .passwd
Welcome1

3 – Create a new file with the following content:

vi ssh.sh
#!/bin/bash

#################################
# Author: Leonardo Santos Lopes #
# Date: 12/05/2021              #
# V1                            #
#################################

GET_SERVER=$1

if [ -z ${GET_SERVER} ]; then
echo "Missing parameter: [IP] or [HOST_NAME], please try again... exiting..."
exit 99
fi

SSH_SERVER=`grep -w ${GET_SERVER} serverlist`

if [ -z $SSH_SERVER ]; then
echo "The following server: [$GET_SERVER], has not been found in the serverlist file."
echo "exiting..."
exit 99
fi

sshpass -f .passwd ssh ${SSH_SERVER}

chmod +x ssh.sh

Now, you can simple execute as below:

./ssh.sh exadata.test.blog

Or if you want something more fancy, you can create an alias, like:

alias go='bash ssh.sh'

And just execute it as below:

go exadata.test.blog

We are done here, enjoy!