Link to home
Start Free TrialLog in
Avatar of aorodrig
aorodrig

asked on

Script to check netstat connections

I have a script, this script check SMPP connection over Port 6200, if I check netstat|grep smpp,
i can see IP ADD connected and UP.

I create a IP_ADD.txt, in order to list  IP Add I need realy check: i.e.

Please review my script and helpme to get a EXCELENTE script.


path=$HOME/Alex
AL=10.2.78.36      # PC Alex
date=`date -u`
arriba=1

while [ $arriba ]
 do
      a=`netstat | grep smpp | grep $SERVER | grep ESTABLISHED`

#subrutina que lee linea por linea un archivo
mylinea ()
  { while read myline
        do
          SERVER=`echo ${myline} | cut -f1 -d","`
          SERVER_INF=`echo ${myline}`
        done
  } < ./ip_add.txt

      if [ $arriba -eq "1" ]
         then
               if [ -z "$a" ]
                 then
                  MSG="SMSC: '$SERVER_INF' is DOWN"
                  for MOBILE in `cat $path/mobiles.txt`
                    do
                    print "submit\n$MOBILE\n\n5710000001\n\n\n\n\n1\n\n\n\n\n\n\n$MSG\ny\nexit\
n\" | sms_operator > /dev/null 2>&1
                    sleep 1
                    done
                  echo "`date` $MSG" >> $path/NetLogChk
                  arriba=1
          if [ $arriba -eq "0" ]
           then
                if [ -n $a ]
                 then
                  MSGS="SMSC: '$SERVER_INF' is UP"
                  for MOBILE in `cat $path/mobiles.txt`
                  do
                  print "submit\n$MOBILE\n\n5710000001\n\n\n\n\n1n\n\n\n\n\n\n$MSGS\ny\nexit\n
\" | sms_operator > /dev/null 2>&1
                  sleep 1
                  done
                  echo "`date` $MSGS" >> $path/NetLogChk
                  arriba=0
                fi
        fi
        sleep 1
done
Avatar of HamdyHassan
HamdyHassan

What is the content of IP_ADD.txt ?

Please post the following
$ cat IP_ADD.txt


Also Does this script is working already, or you still developing...etc
ASKER CERTIFIED SOLUTION
Avatar of yuzh
yuzh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aorodrig

ASKER

This is CAT of IP_ADD.TXT

192.168.28.2 , VoiceMail
192.168.32.3 , Avisor Internet SMS Comtor
192.168.32.4 , Avisor Cita
172.28.3.10 , Avisor Operadora Teledatos
172.28.3.11 , Avisor Operadora Teledatos
198.247.170.47 , Inalambria Swireless
10.2.78.36 , PC Alex
Note I need a dynamic script in order to check a IP add connection and if exist a problem send a short messages to mobile list, and continue reviewing next IP add in IPADD.txt.... this is a loop....
I have another issue when ARRIBA flag change from DOWN to UP, dont send short messages.  Why, may be I have problems with flags or logical states, where
It looks like you have a lot of issues.

Please try what yuzh said, and keep us updated.

I change my function into myscript, but variable SERVER dont get properly.

Its possible get other solution or other script?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I work in KSH, I has changed myscript, this version its works.  but have some problems with ARRIBA variable.

#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''#

path=$HOME/Alex
path2=$HOME/Alex/ip_add.txt
date=`date -u`
arriba=1 export arriba

while [ $arriba ]
 do

i=0 export i
while IFS=',' read ip buf
do
  i=`expr $i + 1`
  echo " ALL file $i ($ip , $buf)"
  SERVER=$ip
  SERVINF="$ip $buf"
  arriba=`expr $arriba`
  a=`netstat | grep smpp | grep $SERVER | grep ESTABLISHED`

      if [ $arriba -eq 1 ]
         then
               if [ -z "$a" ]
                 then
                  MSG="SMSC::SMPP '($SERVINF)' is DOWN"
                  for MOBILE in `cat $path/mobiles.txt`
                    do
                    print "submit\n$MOBILE\n\n5710000001\n\n\n\n\n1\n\n\n\n\n\n\n$MSG\ny\nexit\
n\" | sms_operator > /dev/null 2>&1
                    sleep 1
                    done
                  echo "`date` $MSG" >> $path/NetLogChk
                  arriba=0
          if [ $arriba -eq 0 ]
           then
                if [ -n $a ]
                 then
                  MSGS="SMSC::SMPP '($SERVINF)' is UP"
                  for MOBILE in `cat $path/mobiles.txt`
                  do
                  print "submit\n$MOBILE\n\n5710000001\n\n\n\n\n1\n\n\n\n\n\n\n$MSGS\ny\nexit\n
\" | sms_operator > /dev/null 2>&1
                  sleep 1
                  done
                  echo "`date` $MSGS" >> $path/NetLogChk
                  arriba=1
                fi
        fi
        sleep 1
done < $path2
done
And when i need wite log file  $path/NetLogChk its only print date and i need add $MSG, why dont work?
> arriba=1 export arriba
this is wrong syntax, even in ksh
either use
  arriba=1
  export arriba
or omit the export ('cause you don't need it, did you?)

> .. why dont work?
see:
  arriba=0
  if [ $arriba -eq 0 ]
     then
what should this do, you can omit the if clause
If arriba=0 must check connection until it connect again and netstat|grep be noequal to 0, then perform print and send short messages and change arriba to 1,  but dont work.

Other question, when I want write NetLog File, only print date and dont MSG variable, why?
>  dont MSG variable, why?
your code looks ok, but which of the echo commands does not work?
Start the script with the -x option to see what's going on ..
loohs sx -x

+ print submit\n5715851388\n\n5710000001\n\n\n\n\n1\n\n\n\n\n\n\nSMSC::SMPP '(10.2.78.36   PC A
lex)' is DOWN\ny\nexit\n" | sms_operator > /dev/null 2>&1
                    sleep 1
                    done
                  echo Fri Nov 21 17:38:01 EST 2003 SMSC::SMPP '(10.2.78.36 PC Alex)' is DOWN >
> /usr/EBS/ADCSoftware/smsadm/Alex/NetLogChk
                  arriba=0

and tail NetLog file

Fri Nov 21 15:33:22 EST 2003
Fri Nov 21 16:01:00 EST 2003
Fri Nov 21 16:01:29 EST 2003
Fri Nov 21 17:38:02 EST 2003

Only print date but didnot date + messages

and arriba change to 0 but dont continue with normal sequence, why?
You need to fix a few syntax problems and test it again:

1). as ahoffmann already pointed out:

arriba=1 export arriba
i=0 export i

are wrong !

for ksh, you can use:
export arriba=1

or (this will work for sh/ksh/bash)
arriba=1
export arriba

2) path=$HOME/Alex, will cause some kind of confusion with
ENV var $path, consider change to:

fpath=$HOME/Alex

3) while IFS=',' read ip buf ; do

    change it  to:
    IFS=','
    while read ip buf ;  do

fix up the systax, and then run it in debug more. (put "set -x" in beginning
of your script)


eg:
#!/bin/ksh
set -x

# the body of the script

....




you're using 2 variables: MSG and MSGS
which one is missing?
And again, first fix your syntax problems !
OK I fix parameteres and MSGS varable is not working, when script check IP is UP or connected, this part of myscript dont change variable ARRIBA and dont sens sms.

I has changed path varable but dont writte in LOG FILE.  Only date.
use -x option in hashbang line, or set -x
then check what realy happens, probably post relevant output ..
Please post the newer version of the script, and add

set -x
after the first line of the script, make it looks like:

#!/bin/ksh
set -x

# the body of the script

....


then run it, and post the out put on your screen.

set -x
path1=$HOME/Alex
path2=$HOME/Alex/ip_add.txt
date=`date -u`
arriba=1

while [ $arriba ]
 do

i=0 export i
while IFS=',' read ip buf
do
  i=`expr $i + 1`
  echo " ALL file $i ($ip , $buf)"
  SERVER=$ip
  SERVINF="$ip $buf"
#  arriba=`expr $arriba`
  a=`netstat | grep smpp | grep $SERVER | grep ESTABLISHED`

      if [ $arriba -eq 1 ]
         then
               if [ -z "$a" ]
                 then
                  MSG="SMSC::SMPP '($SERVINF)' is DOWN"
                  for MOBILE in `cat $path1/mobiles.txt`
                    do
                    print "submit\n$MOBILE\n\n5710000001\n\n\n\n\n1\n\n\n\n\n\n\n$MSG\ny\nexit\
n\" | sms_operator > /dev/null 2>&1
                    sleep 1
                    done
                  echo "`date` $MSG" >> $path1/NetLogChk
                  arriba=0
                fi
       fi
          if [ $arriba -eq 0 ]
           then
                if [ -n "$a" ]
                 then
                  MSGS="SMSC::SMPP '($SERVINF)' is UP"
                  for MOBILE in `cat $path1/mobiles.txt`
                  do
                  print "submit\n$MOBILE\n\n5710000001\n\n\n\n\n1\n\n\n\n\n\n\n$MSGS\ny\nexit\n
\" | sms_operator > /dev/null 2>&1
                  sleep 1
                  done
                  echo "`date` $MSGS" >> $path1/NetLogChk
                  arriba=1
                fi
        fi
        sleep 1
done < $path2
done

I just wonder did you read our comments at all?

We alread told you:

"i=0 export i" is WRONG !!!

in your case, you don't need to "export". please read all the comments
carefully,  fix the syntax and try again.


No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Points split  yuzh & ahoffmann

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

liddler
EE Cleanup Volunteer