Solved

Script to check netstat connections

Posted on 2003-11-20
23
605 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:aorodrig
  • 10
  • 5
  • 4
  • +2
23 Comments
 
LVL 9

Expert Comment

by:HamdyHassan
Comment Utility
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
 
LVL 38

Accepted Solution

by:
yuzh earned 250 total points
Comment Utility
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
 

Author Comment

by:aorodrig
Comment Utility
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
 

Author Comment

by:aorodrig
Comment Utility
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
 

Author Comment

by:aorodrig
Comment Utility
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
 
LVL 9

Expert Comment

by:HamdyHassan
Comment Utility
It looks like you have a lot of issues.

Please try what yuzh said, and keep us updated.

0
 

Author Comment

by:aorodrig
Comment Utility
I change my function into myscript, but variable SERVER dont get properly.

Its possible get other solution or other script?
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 250 total points
Comment Utility
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
 

Author Comment

by:aorodrig
Comment Utility
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
 

Author Comment

by:aorodrig
Comment Utility
And when i need wite log file  $path/NetLogChk its only print date and i need add $MSG, why dont work?
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
> 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:aorodrig
Comment Utility
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
>  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
 

Author Comment

by:aorodrig
Comment Utility
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
 
LVL 38

Expert Comment

by:yuzh
Comment Utility
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
you're using 2 variables: MSG and MSGS
which one is missing?
And again, first fix your syntax problems !
0
 

Author Comment

by:aorodrig
Comment Utility
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
use -x option in hashbang line, or set -x
then check what realy happens, probably post relevant output ..
0
 
LVL 38

Expert Comment

by:yuzh
Comment Utility
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
 

Author Comment

by:aorodrig
Comment Utility
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
 
LVL 38

Expert Comment

by:yuzh
Comment Utility
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
 
LVL 18

Expert Comment

by:liddler
Comment Utility
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now