Reading dns info from UNIX shell script into variables for writing to simple text file

Hi all,

I am trying to write a quick UNIX shell script to execute a nslookup and then read the output to a simple file for easy editing. Basically I want the hostname, FQDN host name and IP address to output to the same line for each host in a text file. I've managed to get to the point where I can get the hostname and FQDN hostname outputting as required but I'm not sure how to get the IP address of the host and ignore the IP address of the DNS server.

Any help would be great here's what I have: -

for HOST in host01 host02 host03
do
HOST_DATA=`echo $HOST` ; HOST_DATA2=`nslookup $HOST | grep ${HOST} | awk '{print $2}'`
HOST_DATA3=`nslookup $HOST | grep Address: | awk '{print $2}'`
echo $HOST_DATA $HOST_DATA2 $HOST_DATA3
done

This is the result: -
host01 host01.domain.com 192.168.1.5 192.168.1.1
host02 host02.domain.com 192.168.1.5 192.168.1.2
host03 host03.domain.com 192.168.1.5 192.168.1.3

All I want is to be able to get the IP of each host on the corresponding line so I end up with: -
host01 host01.domain.com 192.168.1.1 # for example but greping for Address: also fives me the DNS server IP address 192.168.1.5 I want to ignore that one.

Thanks as always for your time and assistance.
LVL 29
mass2612Asked:
Who is Participating?
 
ahoffmannConnect With a Mentor Commented:
nslookup $HOST|awk '/^Name/{n=$2}/^Address/{a=$2}($2=="canonical"){c=sprintf("%s %s",c,$1)}END{print '$HOST'" "n" "c" "a}'
0
 
sjm_eeCommented:
It should be easier to work with the "host" command - less output parsing required. If you're certain you want DNS values (rather than /etc/hosts or NIS or NIS+ ones), then you might want to set the resolution order appropriately using the NSORDER environment variable.
0
 
mass2612Author Commented:
Thanks again ahoffmann. As always your help is greatly appreciated. I was able to take your code and figure out how it works and what its doing so hopefully I'll be able to remember how to do this next time.
0
 
jafar78Commented:
Hi,
I took the awk code and wrote the following. It basically reads a list of host names and runs nslookup against each line then writes the ip address to a new file. I hope it helps.

--jh

#!/bin/ksh

set -A line_array
file_name='hostlist.txt'
i=0
while read file_line
do
 line_array[i]=${file_line}
 let i=${i}+1
done < ${file_name}
i=0
count_1=`wc -w < $file_name`
while [ ${i} -le ${#line_array[*]} ]
do
  Line=${line_array[i]}
  if [ -n  "$Line" ]; then
  nslookup ${line_array[i]}|awk '/^Name/{n=$2}/^Address/{a=$2}($2=="canonical"){c=sprintf("%s %s",c,$1)} END{print a >> "testfile.txt"}'
  fi
 let i=$i+1
done
0
 
ahoffmannCommented:
#.. and the unix'sh way to do it :)

sed -e 's#.*#nslookup & |awk '"'"'/^Name/{n=$2}/^Address/{a=$2}($2=="canonical"){c=sprintf("%s%s",c,$1)}END{print "& "n" "c" "a}'"'"'#' hostlist.txt|sh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.