Modification in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modification in shell script
# 1  
Old 01-08-2011
Modification in shell script

Hello Team,

I have prepared script which will check for listening message for ports 1199,1200 and 1201. I need modifcation in script in such a way that if port 1200 is not listening then it should message rmi port 1200 is not listening. Smap for port 1199 and 1201.
kindly guide me to acheive this solution

Code:
#!/usr/bin/ksh

status=`netstat -na | grep 1200 | grep -i listen |sed -n '1p'| awk {'print $7'}`

status1=`netstat -na | grep 1201 | grep -i listen |sed -n '1p'| awk {'print $7'}`

status2=`netstat -na | grep 1199 | grep -i listen |sed -n '1p'| awk {'print $7'}`

for s1 in $status $status1 $status2

if [ $s1 = "LISTEN" ];

then

echo "RMi Server is running fine on server `hostname` @ `date`"

else

echo "RMI Server is not running fine on server `hostname` @`date`" >>/opt/app/rmiserver.txt

fi

# 2  
Old 01-08-2011
try following instead:
Code:
netstat -na| grep 5152 | grep -i listen >/dev/null
status1=`echo $?`
netstat -na | grep 1200 | grep -i listen >/dev/null
status2=`echo $?`
for status in $status1 $status2
do
if [ $status -eq 0 ];then
echo "listening"
else
echo "not listening"
fi
done

Your own code needs following changes:
Code:
for s1 in $status $status1 $status2
do
if [ $s1 = "LISTEN" ];then
echo "RMi Server is running fine on server `hostname` @ `date`"
else
echo "RMI Server is not running fine on server `hostname` @`date`" >>/opt/app/rmiserver.txt
fi
done

# 3  
Old 01-08-2011
Do you get errors?

Post the output of the netstat command.
# 4  
Old 01-08-2011
Hello mtomar,

Thanks for the response. Actually my script is also working fine. I need only modifcation in such a way that if any of the port out of 1200,1201 or 1199 is not listening then it should log messages in file such "rmi port 1200 or 1201 or 1199 is not listening".

---------- Post updated at 04:35 AM ---------- Previous update was at 04:32 AM ----------

Hello Franklin,

I am not getting any error messages. I need only one modifcation in script while checking variable value if any of the varible value ie. status or status1 or status is not matching then it should log messages saying that port 1200 ot port 1199 or port 1201 is not listening while check it;s corresponding variables.
# 5  
Old 01-08-2011
he script will detect if port 1201 (for example) is listen, or if port 12012 is lstening, or if inode 12016 is listening:
Code:
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:1201                0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:12012               0.0.0.0:*                   LISTEN      
<snip>
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     12016  /var/run/cups/cups.sock
unix  2      [ ACC ]     STREAM     LISTENING     11201  /var/run/rpcbind.sock
<snip>

You should do something like:
Code:
   netstat -an | awk '
        BEGIN { p[1199] = 0; p[1200] = 0; p[1201] = 0; }

        $1 != "tcp"    { next; }
        $5 != "LISTEN" { next; }

        $3 == "0.0.0.0:1199" { p[1199]++; }
        $3 == "0.0.0.0:1200" { p[1200]++; }
        $3 == "0.0.0.0:1201" { p[1201]++; }

        END {
            for (i in p) { if (! p[i]) print "port", i, "is not listening"; }
        }
    '

Or if you want to be fancy, change the END block to:
Code:
        s = "";
            for (i in p) { if (! p[i]) s = s " " i; }

        if (s != "") {
            print "ports not listening:" s;
        }
        else {
            print "ok";
        }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to do user-preset login to Bash shell then automate path modification?

How do a user login with full user-environment preset to Bash shell then automatically do path modification with few script codes, either on command-line or put it in a script file. what i tried: bash --login -c PATH="/ANewPath:${PATH}" bash --login -c 'PATH="/ANewPath:${PATH}"; export PATH'... (2 Replies)
Discussion started by: abdulbadii
2 Replies

2. Shell Programming and Scripting

Shell backup script modification

so I'm using this shell script someone made to backup data on a server in an archive (gpg encrypted) and upload to an FTP (meant to be run as a cron job daily). can one of the experts here confirm if the script is fine? It is meant to backup the folder /opt and the sql data i want to know how I... (9 Replies)
Discussion started by: galapagos8000
9 Replies

3. OS X (Apple)

[OS X] Last modification time from shell in CCYYMMDDhhmm.ss format

This example shows last mtime from epoch $ stat -f %m somefile 752911565 But would like to see it like that: 199311100606.05 (2 Replies)
Discussion started by: Tribe
2 Replies

4. Shell Programming and Scripting

Modification in script

Hi, I have below script, i want to monitor that that ntp server listed in setting is under sync or not. I wrote below script but it is not working properly. Here are problems, first it should server under sync if "*" shows and rest if shows "+" it means it is next server in waiting list.... (4 Replies)
Discussion started by: learnbash
4 Replies

5. Shell Programming and Scripting

Needed shell script to read txt file and do some modification

Hi ...programmers... I need a shell script to perform some specific task.. my txt file looks like this netcdf new { dimensions: XAX1_11 = 11 ; variables: double XAX1_11(XAX1_11) ; XAX1_11:point_spacing = "even" ; XAX1_11:axis = "X" ; float DEPTH(XAX1_11) ;... (19 Replies)
Discussion started by: Akshay Hegde
19 Replies

6. Shell Programming and Scripting

Help with Shell Script Modification

Hi all Iam very new to Shell Scripting, I have to modify a shell script looking at an existing one except that it will query against some table X in A database. Befor Spooling check if there are any reload files if there archive the files. The above scipt executes some abc.sql which will b a new... (2 Replies)
Discussion started by: Varunkv
2 Replies

7. Shell Programming and Scripting

Need a modification on this script

Hi All I have files contains rows which look like this: 2 20090721_16:58:47.173 JSUD2 JD1M1 20 IAM 966591835270 249918113182 b 3610 ACM b 3614 ACM b 3713 CPG b 3717 CPG f 5799 REL b 5815 RLC b 5817 RLC :COMMA: NCI=00,FCI=6101,CPC=0A,TMR=00,OFI=00,USI: :COMMB: BCI=1234: :RELCAUSE:10: ... (1 Reply)
Discussion started by: zanetti321
1 Replies

8. UNIX for Dummies Questions & Answers

File comparision and modification using shell script

Hello everyone, I would like to know how to compare two files and modify any differences with some other data using shell script. I think it would be better understood with an example. I got two files named 'filex' and filey'. 'filex' is constant file without any changes in data. 'filey' is... (2 Replies)
Discussion started by: maddy81
2 Replies

9. Shell Programming and Scripting

crontab entry modification using shell script

hi Friends, iam trying to write a script which will grep the particular entry in crontab of root and enable/disable it .iam a novice in scripting. your suggestions are most welcome..please help Cheers!! (4 Replies)
Discussion started by: munishdh
4 Replies

10. UNIX for Dummies Questions & Answers

Shell script: last modification date for a file

Hi i have a ques in Shell scripting: ques: accept a filename as a command line argument. Validate the input and display the last modification date for that file. Help pls. (4 Replies)
Discussion started by: onlyc
4 Replies
Login or Register to Ask a Question