Friday, October 7, 2011

Shell Script to monitor the services and ports running in the linux system in the different Format

The Script read the service running the Linux system in the different  format

The Script read the port number from the sample.txt  file and service name

 sample .txt#INSTANCE_NAME:PORT:USER:EXTERNAL_PORT
http 80 root 81
https 443 root
ssh 22 rrot mail 25 rrotftp 21 root  
#!/bin/bash

cat /root/sample.txt|grep -v "^#"| while read line
do
#echo $line

NETSTAT=/bin/netstat
# for old file
INSTANCE_NAME=`echo $line | awk '{print $1}'`
PORT_NO=`echo $line | awk '{print $2}'`
USER=`echo $line | awk '{print $3}'`
# for new file
#INSTANCE_NAME=`echo $line | awk -F':' '{ print $1 }'`
#PORT_NO=`echo $line | awk -F':' '{ print $2 }'`
#USER=`echo $line | awk -F':' '{ print $3 }'`
echo $INSTANCE_NAME
echo  $PORT_NO
echo $USER
RPORTS=$($NETSTAT -utan|awk '{print $4}'
|sed -s "s/::ffff:[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]/::/g"
|sed -s "s/0.0.0.0/::/g"|sed -s "s/127.0.0.1/::/g"|sed -s "s/[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]/::/g"|
cut -f4 -d":"|grep -v 127.0.0.1)

  status="NO"
  for r in $RPORTS
  do
   if [ "$r" == "$PORT_NO" ]
   then
    status="YES"
    break
   fi
  done
  echo -n " $INSTANCE_NAME is running $status with user as $USER"
  echo ""
done
 
 
it monitor the services 
0.0.0.0:80
 :::8080
::ffff:127.0.0.1:8005 

No comments:

Post a Comment