1828219 Members
1781 Online
109975 Solutions
New Discussion

Re: shell script

 
SOLVED
Go to solution
Shivkumar
Super Advisor

shell script

Dear Sirs;

I need a script for the following situation.

Want to run nslookup xx.xx.xx.xx ( this is a virtual ip address )
if it returns
12.16.91.93
Then no problem and output should go to a file for recording
if it returns
14.16.17.22
then it should say "problem" and send the email alert to xxyy.zz@abc.com
(sendmail is already running on the box)
The message should also get recorded in the above mentioned file

check for the size of this file if size is say more than 2mb; it should delete the file and record in a new file

this script should be running after every 25 seconds.

I don't want to run this script as a cron job.

Please suggest.

Thanks
Shiv
15 REPLIES 15
Devender Khatana
Honored Contributor

Re: shell script

Hi,

Scripting part is seperate but I do not think it would be possible to schedule it without cron. Also the frequency is also very less as minimum for the cron to work is 1 minute.

Just curious to see some responses for the same.

Regards,
Devender
Impossible itself mentions "I m possible"
Pete Randall
Outstanding Contributor

Re: shell script

The scheduling can be done with at, but not at 25 second intervals. Like cron, the smallest interval at deals with is minutes.

echo "script_name" | at now + 1 minute


Pete

Pete
Leif Halvarsson_2
Honored Contributor

Re: shell script

Hi,
I remember a similar problem for some time ago. A possible solution is to run the script from inittab. Put a sleep 25 statment at the end of the script and use the respawn option in inittab (see /etc/inittab for examples).
cilene Peterson
New Member

Re: shell script

I would create a perl script using exec or eval to execute your script, sleep for 25 seconds and execute again...

Good luck!
cilene Peterson
New Member

Re: shell script

I would create a perl script using exec or eval to execute your script, sleep for 25 seconds and execute again...

Of course this would be a loop forever program...

Good luck!
Shivkumar
Super Advisor

Re: shell script

Can someone please suggest the shell script code ?

Thanks and regards,
Shiv
Mel Burslan
Honored Contributor
Solution

Re: shell script

while true
do #start of forever loop
IPADR=`nslookup | grep Address | awk {'print $2'}`
if [ "$IPADR" = "12.16.91.93" ]
then
(printf "Everything is fine at ";date) >> /tmp/mylogfile
else
if [ "$IPADR" = "14.16.17.22" ]
then
echo problem | sendmail xxyy.zz@abc.com
(echo "PROBLEM ENCOUNTERED at ";date) >> /tmp/mylogfile
else
(echo "UNKNOWN IP RETURNED at ";date) >> /tmp/mylogfile
fi
fi
sleep 25

LOGSIZE=`ll /tmp/mylogfile | awk {'print $5'}`
if [ $LOGSIZE -ge 2000000 ]
then
cat /dev/null > /tmp/mylogfile # zero it out
fi


done # repeat forever

if I were you I would move the logfile to .old extension for future examination and then zero it out, but this is only my opinion


hope this helps
________________________________
UNIX because I majored in cryptology...
Tom Danzig
Honored Contributor

Re: shell script

Just a thought ... Mel's script will keep sending an email out every 25 seconds until the issue is corrected. You may want to tailor the code to do otherwise.
Howard Marshall
Regular Advisor

Re: shell script

This is not a super challenging script but to write it you need to specify a few more things

I assume you are just testing your DNS server or servers and actually fixing that would be the real solution but till then lets get on with it.

First you want a script that is going to loop every 25 seconds or so but you probably don't want it to loop for ever and ever till the end of time. That can be done with a stop file in the /tmp directory

#!ksh

while [ ! -f /tmp/mystopfile ]
do


#do some stuff


sleep 25
done

now all you have to do is touch /tmp/mystop and the loop will drop out and stop the script

next, the business end doing the nslookup and deciding based on the results what to do

I don't know what nslookup version you are using or what switches so I will leave up to you to filter down to just the IP address

IPTRACKERLOG=/tmp/iptrackerlog
If [ wc -l $IPTRACKERLOG > 1000 ]
then
echo "log restart `date` " > $IPTRACKERLOG
fi

IPADDR=`nslookup xx.xx.xx.xx | grep Address | cut c11-`

If [ $IPADDR = "12.16.91.93" ]
Then
Echo "all is well with IP $IPADDR `date`" >> $IPTRACKERLOG

#I assume anything but this response is cause for concern so
else
echo "ERROR >>> tracker found IP $IPADDR `date`" >> $IPTRACKERLOG

#you may want to put a stop file here too and have it set its own otherwise you will get a message every 25 seconds till the script stops or the problem is fixed.
echo "your ip tracker thing on the hpux server got an error" | mailx -s "your ip check script" usr@address.com
fi

legal notice: I don't guarantee any of these commands to work the way they are listed they are here for novelty purposes only and should never actually be used till you have tested and modified them to suit your needs and even then, if it all goes bad, its still not my fault.
Muthukumar_5
Honored Contributor

Re: shell script

You can try as,

#!/usr/bin/ksh
stime=25

# User input
ipaddress=""
logfile=/tmp/iplookup.log
mailID="xxyy.zz@abc.com"

while [ 1 ]
do

nslookup $ipaddress | grep -qE '12.16.91.93'
if [ $? -eq 0 ]
then
echo "Lookup Success @ $(date)" >> $logfile
fi

nslookup $ipaddress | grep -qE '14.16.17.22'
if [ $? -eq 0 ]
then
echo "Lookup Failed @ $(date)" >> $logfile
echo "Lookup Problem @ $(date)" | mailx -s"Problem" ${mailID}
fi

ret=$(ls -l $logfile | awk '{ size=$5/1048576; if ( size > 2 ) { print "1"; } else { print "2";}}')

if [ $ret -eq 1 ]
then
cat $logfile >> $logfile.old
fi

sleep $stime

done

# END
exit 0

hth.
Easy to suggest when don't know about the problem!
Shivkumar
Super Advisor

Re: shell script

Muthukumar; i used your script.
i got the output as :
Lookup Success @ Mon Oct 17 23:11:25 PDT 2005
Lookup Success @ Mon Oct 17 23:11:50 PDT 2005

now suppose i want to show ip address also against success and failure in the log file then how to modify this script ?

Thanks,
Shiv
Muthukumar_5
Honored Contributor

Re: shell script

do you want to store success results in one file and failure results in one file so that you can keep track?

Without this, you can grep like,

grep 'Success' /tmp/iplookup.log
grep 'Failed' /tmp/iplookup.log

to get results.

If it is not your requirement then revert here.

thx.
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: shell script

nslookup $ipaddress | grep -qE '12.16.91.93'
if [ $? -eq 0 ]
then
echo "Lookup Success for $ipaddress @ $(date)" >> $logfile
fi

nslookup $ipaddress | grep -qE '14.16.17.22'
if [ $? -eq 0 ]
then
echo "Lookup Failed for $ipaddress @ $(date)" >> $logfile
echo "Lookup Problem for $ipaddress @ $(date)" | mailx -s"Problem" ${mailID}
fi
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: shell script

great arun ;)

Just change script echo statements with ${ipaddress} to get your desired output with IP-Address.

hth.

Easy to suggest when don't know about the problem!
rmueller58
Valued Contributor

Re: shell script

Shiv,

I use a process to page me in the event our primary DNS server fails..

if the NS lookup fails to return xxx.xxx.xxx.xxx then I have the system send me a page via my email pager.. Works great.


#!/bin/bash
ns=`nslookup -sil www.yourwebserver.xxx |head -1 |awk '{print $2}'`
if [ "$ns" != "xxx.xxx.xxx.xxx" ]
then
echo "Lookup failed" > lookup.txt
mail -v -s "xxx.xxx.xxx.xxx failed `date +%Y%m%d:%H%M`" name@email.xxx < lookup.txt
fi