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 "$(date) Starting the SSH Connection Check...."
#set -x
read -rsp $'Please Enter password below: ' SSHPASS
export SSHPASS
SSH_Connection () {
hostTarget=${1}
sshpass -e ssh -q "${hostTarget}" "true" -o StrictHostKeyChecking=no -o ConnectTimeout=60 2>>/dev/null
if (( $? -eq 0 ))
then
echo "$CurrntTime $MachineName : SSH connection is Up"
elif (( $? -eq 255 ))
then
echo "$CurrntTime $MachineName : SSH Authentication Failed"
elif (( $? -eq 2 ))
then
echo "$CurrntTime $MachineName : SSH connection is Down"
elif (( $? -eq 1 ))
then
echo "$CurrntTime $MachineName : SSH Authentication Failed"
else
echo "$CurrntTime $MachineName : SSH connection is Down"
fi
}
HostList=$(<~/host2)
CurrntTime=$(date +'%m/%d/%Y %T')
for MachineName in ${HostList}
do
SSH_Connection "${MachineName}" &
done
# wait for all outstanding background jobs to complete before continuing
wait
# at last clear the exported variable containing the password
unset SSHPASS