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

Posted on 2007-08-06
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: -

All I want is to be able to get the IP of each host on the corresponding line so I end up with: -
host01 # 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

    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

    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

    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

    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

    #.. 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

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    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…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now