Solved

Script to check netstat connections

Posted on 2003-11-20
23
614 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
ID: 9791136
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
ID: 9792162
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
ID: 9797197
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aorodrig
ID: 9797266
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
ID: 9797312
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
ID: 9797407
It looks like you have a lot of issues.

Please try what yuzh said, and keep us updated.

0
 

Author Comment

by:aorodrig
ID: 9797789
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
ID: 9799812
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
ID: 9799897
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
ID: 9799909
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
ID: 9800172
> 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
 

Author Comment

by:aorodrig
ID: 9800364
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
ID: 9800498
>  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
ID: 9800558
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
ID: 9801866
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
ID: 9802222
you're using 2 variables: MSG and MSGS
which one is missing?
And again, first fix your syntax problems !
0
 

Author Comment

by:aorodrig
ID: 9811950
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
ID: 9812031
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
ID: 9815947
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
ID: 9818319
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
ID: 9818542
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
ID: 10191763
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 71
Detect CR LF to each line 12 174
Sed question 2 122
Bartender label printing - switch on and off graphics 3 71
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

713 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