Operating System - Linux
1756030 Members
4442 Online
108839 Solutions
New Discussion юеВ

nslookup shell script with input and output files

 
SOLVED
Go to solution
jamesatwork
Occasional Advisor

nslookup shell script with input and output files

Hello! Please help!
I have a slight dilema, the data in my forward and reverse zones differ quite a bit. How would I write a shell script that would take an input file list of fqdn or ip and run nslookup on them and output a log file based on the results? I wrote one a while ago for batch but I am just learning unix/shell.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=960329

Is very close to what I need. Thanks in advance!
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: nslookup shell script with input and output files

Hi:

This could be as simple as:

# cat .mylookup.sh
while read ADDR
do
nslookup ${ADDR} | tee -a mylookup.log
done < mylookup

...where 'mylookup' is a file with one IP or FQDN per line:

10.10.11.12
host01.xyz.net
host02.xyz.net

...run as:

# ./mylookup.sh

...Your output will be written to your terminal and to the 'mylookup.log' file.

Regards!

...JRF...
jamesatwork
Occasional Advisor

Re: nslookup shell script with input and output files

ok great! that works well, is it possible to make a tweek request, it should not be more than a quick if statement, I don't know how errors work in nix world yet but if name or ip is resolvable can the file have
- resolves
and if it doesn't
- does not resolve
Thanks so far, your help is very much appreciated!
James R. Ferguson
Acclaimed Contributor
Solution

Re: nslookup shell script with input and output files

Hi (again):

OK, here's a simple hack:

# cat ./mylookup
#!/usr/bin/sh
while read ADDR
do
RESULT=`nslookup ${ADDR} | grep -E "^Address:"`
if [ ! -z "${RESULT}" ]; then
echo "${ADDR} is_ok" | tee -a mylookup.log
fi
done < mylookup

...run this just as you did the first version. Good results have a line in their output that begins with "Address:". The caret (^) anchors the pattern to match to the beginning of the line. Thus only lines that start with "Address:" are captured. If nothing matches, then 'nslookup' reports the target in the error returned, so we just print that.

Have a look at the manpages for 'nslookup'.

Regards!

...JRF...
jamesatwork
Occasional Advisor

Re: nslookup shell script with input and output files

thank you!! that is the start I needed, cheers!