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
aorodrigAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HamdyHassanCommented:
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
0
yuzhCommented:
When you read a file in a script, you should use the FULL PATH to the file.

use:  /path-to/ip_add.txt instead of ./ip_add.txt

./ip_add.txt only work when you start the script in the same dir as ip_add.txt !


You could also use:

IPADD=/path-to/ip_add.txt

#Function
mylinea ()
{
exec 0 <$IPADD
while read myline
       do
         SERVER=`echo ${myline} | cut -f1 -d","`
         SERVER_INF=`echo ${myline}`
       done
}

if you need more help, please post the format of ip_add.txt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aorodrigAuthor Commented:
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
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

aorodrigAuthor Commented:
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....
0
aorodrigAuthor Commented:
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
0
HamdyHassanCommented:
It looks like you have a lot of issues.

Please try what yuzh said, and keep us updated.

0
aorodrigAuthor Commented:
I change my function into myscript, but variable SERVER dont get properly.

Its possible get other solution or other script?
0
ahoffmannCommented:
1. variable SERVER not set before first use
2. definition of function within other shell code

think this is what yuzh already suggested

> Its possible get other solution or other script?
yes as much as you can imagine
pero no tengo idea qué shell es preferado
;-)
0
aorodrigAuthor Commented:
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
0
aorodrigAuthor Commented:
And when i need wite log file  $path/NetLogChk its only print date and i need add $MSG, why dont work?
0
ahoffmannCommented:
> 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
0
aorodrigAuthor Commented:
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?
0
ahoffmannCommented:
>  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 ..
0
aorodrigAuthor Commented:
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?
0
yuzhCommented:
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

....




0
ahoffmannCommented:
you're using 2 variables: MSG and MSGS
which one is missing?
And again, first fix your syntax problems !
0
aorodrigAuthor Commented:
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.
0
ahoffmannCommented:
use -x option in hashbang line, or set -x
then check what realy happens, probably post relevant output ..
0
yuzhCommented:
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.

0
aorodrigAuthor Commented:
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

0
yuzhCommented:
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.


0
liddlerCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.