Automate ports connectivity check using telnet and timeout

Check ports connectivity using automation with telnet and timeout commands. Timeout will help us not get blocked for a long time. Adjust the timeout value on case to case basis,

# vi ~/check_my_server_hostname.sh

timeout 2 bash -c "echo 'exit' | telnet my_server_hostname.tld 80"
if [ $? -eq 1 ];
then
        # echo "good";
        echo "Connectivity check succeeded" | mail -s "Connected - $HOSTNAME to my_server_hostname.tld:80" -r [email protected] [email protected]
else
        # echo "bad";
        echo "Connectivity check failed"  | mail -s "Not connected - $HOSTNAME to my_server_hostname.tld:80" -r [email protected] [email protected]
fi

Setup crontab schedule. Below schedule with check connectivity every hour, past 15 and 45 mins.

# crontab -e
15,45 * * * *  ~/check_my_server_hostname.sh

 

Quickly check ports

for host in 127.0.0.1 127.0.0.2 127.0.0.3
do
  timeout 2 bash -c "echo 'exit' | telnet $host 80"
done

Alternative, using hostname:port format list

for ep in 127.0.0.1:80 127.0.0.1:81
do
 timeout 2 bash -c "echo 'exit' | telnet ${ep%:*} ${ep#*:}"
done

 

 

HTH

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *