Operating System - HP-UX
1752587 Members
4664 Online
108788 Solutions
New Discussion юеВ

Shell script to email about nslookup failure

 
Tharshini
New Member

Shell script to email about nslookup failure

I'm currently writing a shell script to do nslookup on a list of servers which could be obtained from listener file. Could anyone please help me how to send an email notification in case nslookup failed for a server?
11 REPLIES 11
James R. Ferguson
Acclaimed Contributor

Re: Shell script to email about nslookup failure

Hi:

cat ./nsfailure
#!/usr/bin/sh
HOST=$1
RSLT=$(nslookup $1 | sed -ne "/can.t find/p")
[ -z "${RSLT}" ] || mailx -s "nslookup failed: ${RSLT}" root < /dev/null
exit

# ./nsfailure host_or_IP

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Shell script to email about nslookup failure

Hi (again):

Oops, we need to capture STDERR, so:

# cat ./nsfailure
#!/usr/bin/sh
HOST=$1
RSLT=$(nslookup $1 2>&1 | sed -ne "/can.t find/p")
[ -z "${RSLT}" ] || mailx -s "nslookup failed: ${RSLT}" root < /dev/null
exit

# ./nsfailure host_or_IP

Regards!

...JRF...
Tharshini
New Member

Re: Shell script to email about nslookup failure

Hi James,

Thanks so much for your help. I'm modifying the script at the moment and it will be ready to go by tomorrow.

I will let you know about the result.

Thanks so much again!

Best Regards,
Tharshini
James R. Ferguson
Acclaimed Contributor

Re: Shell script to email about nslookup failure

Hi (again):

Since you are new to this forum, welcome!

If/when you are satisfied with the answers you have received, please read:

http://forums.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
Tharshini
New Member

Re: Shell script to email about nslookup failure

Hi James,

When I tried to run the script, it did not accept and gave this error continuously.
`RSLT=$' unexpected.

This is the script:
#!/bin/sh

cat /disk1r1/app/oracle/product/10.2.0/db_1/network/admin/sqlnet.ora | cut -d= -f3 | grep -v 10 | grep -v 192 | grep -v ")" | sed 's/\(.*\)./\1/' >> output.txt

log="/apps/dba/bin/output.txt"; export log
rm log

for HOSTNAME in `more log`
do
RSLT=$(nslookup $HOSTNAME 2>&1 | sed -ne "/can.t find/p")
[ -z "${RSLT}" ] || mailx -s "nslookup failed: ${RSLT}" root < /dev/null
done

exit

Thanks for helping me out.

Best Regards,
Tharshini
James R. Ferguson
Acclaimed Contributor

Re: Shell script to email about nslookup failure

Hi:

> When I tried to run the script, it did not accept and gave this error continuously.
`RSLT=$' unexpected.

What shell are you running on what HP-UX release? '/bin/sh' should point to '/usr/bin/sh' which should be the POSIX shell.

The notation:

RSLT=$(nslookup ${HOST} 2>&1 | sed -ne '/can.t find/p')

...is a better (POSIX) form of the archaic back-ticks:

RSLT=`(nslookup ${HOST} 2>&1 | sed -ne '/can.t find/p'`

See if the back-tick syntax fixes your problem.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Shell script to email about nslookup failure

>it did not accept and gave this error continuously
>for HOSTNAME in `more log`

You shouldn't be using more(1) here, that's for interactive use. Instead use:
for HOSTNAME in $(< log); do

>JRF: See if the back-tick syntax fixes your problem.

You left in a "(":
RSLT=`nslookup ${HOST} 2>&1 | sed -ne '/can.t find/p'`
James R. Ferguson
Acclaimed Contributor

Re: Shell script to email about nslookup failure

Hi (again):

> Dennis: You shouldn't be using more(1) here, that's for interactive use. Instead use:
for HOSTNAME in $(< log); do

If Tharshini is running a non-POSIX shell, I think that this too will be an issue. I would suggest the alternative:

for HOSTNANE in `cat log`; do

> Dennis > JRF: You left in a "(":
RSLT=`nslookup ${HOST} 2>&1 | sed -ne '/can.t find/p'`

Yes indeed I did. Thanks for catching that.

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor

Re: Shell script to email about nslookup failure

>JRF: If Tharshini is running a non-POSIX shell,

But Tharshini said: This is the script:
#!/bin/sh