-->
🏠 🔍
SHAREOLITE

Beginners Linux shell script to check whether a TCP connection is up or down

Below mentioned is a ready to use Linux script to check the TCP connection status and alert if connection is down. The script uses linux netstat command to capture the details . 



Copy the code below highlighted in blue , save it in a file and execute for output. Configure the source IP under variable sourceIP and remote IP under variable remoteIP




#!/bin/sh
sourceIP=10.192.10.2
remoteIP=200.12.34.12

echo -e "\n\033[1;44m              TCP connection checker               \033[0m \n"
netstat -tanp  > /tmp/netstat_result.txt 2>&1
cat /tmp/netstat_result.txt |grep "$sourceIP" | grep "$remoteIP" |grep 'ESTABLISHED'  >> /dev/null  2>&1
if [ $? -eq 0 ]
then
echo -e "\033[1;32m TCP connection between $sourceIP and $remoteIP is established\033[0m"
else
echo -e "\033[1;31m TCP connection between $sourceIP and $remoteIP is down!\033[0m"
fi

Hope this helps some Linux script beginners.
Comments

–>