Modification in shell script

Hello Team,

I have prepared script which will check for listening message for ports 1199,1200 and 1201. I need modifcation in script in such a way that if port 1200 is not listening then it should message rmi port 1200 is not listening. Smap for port 1199 and 1201.
kindly guide me to acheive this solution

#!/usr/bin/ksh

status=`netstat -na | grep 1200 | grep -i listen |sed -n '1p'| awk {'print $7'}`

status1=`netstat -na | grep 1201 | grep -i listen |sed -n '1p'| awk {'print $7'}`

status2=`netstat -na | grep 1199 | grep -i listen |sed -n '1p'| awk {'print $7'}`

for s1 in $status $status1 $status2

if [ $s1 = "LISTEN" ];

then

echo "RMi Server is running fine on server `hostname` @ `date`"

else

echo "RMI Server is not running fine on server `hostname` @`date`" >>/opt/app/rmiserver.txt

fi

try following instead:

netstat -na| grep 5152 | grep -i listen >/dev/null
status1=`echo $?`
netstat -na | grep 1200 | grep -i listen >/dev/null
status2=`echo $?`
for status in $status1 $status2
do
if [ $status -eq 0 ];then
echo "listening"
else
echo "not listening"
fi
done

Your own code needs following changes:

for s1 in $status $status1 $status2
do
if [ $s1 = "LISTEN" ];then
echo "RMi Server is running fine on server `hostname` @ `date`"
else
echo "RMI Server is not running fine on server `hostname` @`date`" >>/opt/app/rmiserver.txt
fi
done 

Do you get errors?

Post the output of the netstat command.

Hello mtomar,

Thanks for the response. Actually my script is also working fine. I need only modifcation in such a way that if any of the port out of 1200,1201 or 1199 is not listening then it should log messages in file such "rmi port 1200 or 1201 or 1199 is not listening".

---------- Post updated at 04:35 AM ---------- Previous update was at 04:32 AM ----------

Hello Franklin,

I am not getting any error messages. I need only one modifcation in script while checking variable value if any of the varible value ie. status or status1 or status is not matching then it should log messages saying that port 1200 ot port 1199 or port 1201 is not listening while check it;s corresponding variables.

he script will detect if port 1201 (for example) is listen, or if port 12012 is lstening, or if inode 12016 is listening:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:1201                0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:12012               0.0.0.0:*                   LISTEN      
<snip>
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     12016  /var/run/cups/cups.sock
unix  2      [ ACC ]     STREAM     LISTENING     11201  /var/run/rpcbind.sock
<snip>

You should do something like:

   netstat -an | awk '
        BEGIN { p[1199] = 0; p[1200] = 0; p[1201] = 0; }

        $1 != "tcp"    { next; }
        $5 != "LISTEN" { next; }

        $3 == "0.0.0.0:1199" { p[1199]++; }
        $3 == "0.0.0.0:1200" { p[1200]++; }
        $3 == "0.0.0.0:1201" { p[1201]++; }

        END {
            for (i in p) { if (! p) print "port", i, "is not listening"; }
        }
    '   

Or if you want to be fancy, change the END block to:

        s = "";
            for (i in p) { if (! p) s = s " " i; }

        if (s != "") {
            print "ports not listening:" s;
        }
        else {
            print "ok";
        }