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

Posted on 2007-08-06
Medium Priority
Last Modified: 2013-12-26
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
HOST_DATA=`echo $HOST` ; HOST_DATA2=`nslookup $HOST | grep ${HOST} | awk '{print $2}'`
HOST_DATA3=`nslookup $HOST | grep Address: | awk '{print $2}'`

This is the result: -
host01 host01.domain.com
host02 host02.domain.com
host03 host03.domain.com

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 # for example but greping for Address: also fives me the DNS server IP address I want to ignore that one.

Thanks as always for your time and assistance.
Question by:mass2612
LVL 14

Expert Comment

ID: 19637340
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.
LVL 51

Accepted Solution

ahoffmann earned 2000 total points
ID: 19637448
nslookup $HOST|awk '/^Name/{n=$2}/^Address/{a=$2}($2=="canonical"){c=sprintf("%s %s",c,$1)}END{print '$HOST'" "n" "c" "a}'
LVL 29

Author Comment

ID: 19643387
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.

Expert Comment

ID: 19671499
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.



set -A line_array
while read file_line
 let i=${i}+1
done < ${file_name}
count_1=`wc -w < $file_name`
while [ ${i} -le ${#line_array[*]} ]
  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"}'
 let i=$i+1
LVL 51

Expert Comment

ID: 19671923
#.. 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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question