I need to check if telnet is happening or not on multiple remote servers.
I wrote a while loop to SSH over multiple remote servers and triggers an email whenever the telnet fails. But the issue is while loops iterates only over the first server and exits out of the script without reading remaining servers. Below is my shell script
#!bin/bash
while read host_details
do
USERNAME=$(echo $host_details | awk -F"|" '{print $1}')
HOSTIP=$(echo $host_details | awk -F"|" '{print $2}')
PORT=$(echo $host_details | awk -F"|" '{print $3}')
PROXY=$(echo $host_details | awk -F"|" '{print $4}')
PROXY_PORT=$(echo $host_details | awk -F"|" '{print $5}')
STATUS=$(ssh -n ${USERNAME}@${HOSTIP} -p${PORT} "timeout 4 bash -c \"</dev/tcp/${PROXY}/${PROXY_PORT}\"; echo $?;" < /dev/null)
if [ "$STATUS" -ne 0 ]
then
echo "Connection to $PROXY on port $PROXY_PORT failed"
mutt -s "Telnet connection to $PROXY on port $PROXY_PORT failed" abc.def@xyz.com
else
echo "Connection to $PROXY on port $PROXY_PORT succeeded"
mutt -s "Telnet connection to $PROXY on port $PROXY_PORT succeeded" abc.def@xyz.com
fi
done<.hostdetails
I observed my script works only when I remove IF condition and my while loops iterates over all the servers as expected.
Can anyone suggest why when I use IF condition the script exits after first iteration? And how to make it work and get the email alerts?