Dirty Bash hack to run the ssh connections in the Parallel mode A Few years back I used to opt the below method to run the shell program in a Parallel way to open multiple ssh session at a time to get a quick result.
I know there are various other good utilities available, however due to some pressing needs I've to use this way, this dirty piece of code works well however I would like to know if there are some suggestion to improve it further considering the fact I have to use it for some compelling reasons over other better tools.
#!/bin/bash
echo &quot;$(date) Starting the SSH Connection Check....&quot;
#set -x

read -rsp $'Please Enter password below: ' SSHPASS
export SSHPASS
SSH_Connection () {
    hostTarget=${1}
    sshpass -e ssh -q &quot;${hostTarget}&quot; &quot;true&quot; -o StrictHostKeyChecking=no -o ConnectTimeout=60 2&gt;&gt;/dev/null
    if [[ $? -eq 0 ]]
    then
        echo  &quot;$CurrntTime $MachineName   : SSH connection is Up&quot;
    elif [[ $? -eq 255 ]]
        then
        echo  &quot;$CurrntTime $MachineName   : SSH Authentication Failed&quot;
    elif [[ $? -eq 2 ]]
        then
        echo  &quot;$CurrntTime $MachineName   : SSH connection is Down&quot;
    elif [[ $? -eq 1 ]]
        then
        echo  &quot;$CurrntTime $MachineName   : SSH Authentication Failed&quot;
    else
        echo  &quot;$CurrntTime $MachineName   : SSH connection is Down&quot;
    fi
}

HostList=$(&lt;~/host2)

CurrntTime=$(date +'%m/%d/%Y %T')

for MachineName in ${HostList}
do
    SSH_Connection &quot;${MachineName}&quot; &amp;
done
# wait for all outstanding background jobs to complete before continuing
wait
# at last clear the exported variable containing the password
unset SSHPASS
The only part I would change are the chained if statements. Use case:
CurrntTime=&quot;some_timestamp&quot;
MachineName=&quot;some_host&quot;
for retval in 0 255 2 1 8; do
   echo &quot;Testing exitcode ${retval}&quot;
   case ${retval} in
   0)
        echo  &quot;$CurrntTime $MachineName   : SSH connection is Up&quot;;;
   255)
        echo  &quot;$CurrntTime $MachineName   : SSH Authentication Failed&quot;;;
   2)
        echo  &quot;$CurrntTime $MachineName   : SSH connection is Down&quot;;;
   1)
        echo  &quot;$CurrntTime $MachineName   : SSH Authentication Failed&quot;;;
   *)
        echo  &quot;$CurrntTime $MachineName   : SSH connection is Down&quot;;;
   esac
done

More optimisation is not needed. The alternative beneath is slightly shorter but makes the code harder to read (more fun, though):
CurrntTime=&quot;some_timestamp&quot;
MachineName=&quot;some_host&quot;

ssh_err_0=&quot;connection is Up&quot;
ssh_err_1=&quot;Authentication Failed&quot;
ssh_err_2=&quot;connection is Down&quot;
ssh_err_255=&quot;Authentication Failed&quot;
ssh_err_other=&quot;connection is Down&quot;

for retval in 0 255 2 1 8; do
   echo &quot;Testing exitcode ${retval}&quot;
   printf &quot;%s&quot; &quot;$CurrntTime $MachineName   : SSH &quot;
   case ${retval} in
   0|1|2|255)
      message=&quot;ssh_err_${retval}&quot;
      echo &quot;${!message}&quot;;;
   *)
      echo &quot;${ssh_err_other}&quot;;;
   esac
done

When you use the first case in the total script, and move the SSH_Connection() function to the start of the script, it would be
#!/bin/bash

SSH_Connection () {
    hostTarget=${1}
    sshpass -e ssh -q &quot;${hostTarget}&quot; &quot;true&quot; -o StrictHostKeyChecking=no \
       -o ConnectTimeout=60 2&gt;/dev/null

    case $? in
    0)
       echo  &quot;$CurrntTime $hostTarget: SSH connection is Up&quot;;;
    255)
       echo  &quot;$CurrntTime $hostTarget: SSH Authentication Failed&quot;;;
    2)
       echo  &quot;$CurrntTime $hostTarget: SSH connection is Down&quot;;;
    1)
       echo  &quot;$CurrntTime $hostTarget: SSH Authentication Failed&quot;;;
    *)
       echo  &quot;$CurrntTime $hostTarget: SSH connection is Down&quot;;;
    esac
}

echo &quot;$(date) Starting the SSH Connection Check....&quot;

read -rsp $'Please Enter password below: ' SSHPASS
export SSHPASS

HostList=$(&lt;~/host2)

CurrntTime=$(date +'%m/%d/%Y %T')

for MachineName in ${HostList}; do
   SSH_Connection &quot;${MachineName}&quot; &amp;
done
# wait for all outstanding background jobs to complete before continuing
wait
# at last clear the exported variable containing the password
unset SSHPASS

I did not move the CurrentTime=$(..) inside the function. When you want to see the date/time after the ssh is finished, then you will need to store the exit code in a variable:
...
sshpass -e ssh ...
retval=$?
CurrentTime=$(date +'%m/%d/%Y %T')
case ${retval} in ...