?
Solved

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

Posted on 2007-08-06
6
Medium Priority
?
2,528 Views
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
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.
0
Comment
Question by:mass2612
5 Comments
 
LVL 14

Expert Comment

by:sjm_ee
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.
0
 
LVL 51

Accepted Solution

by:
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}'
0
 
LVL 29

Author Comment

by:mass2612
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.
0
 

Expert Comment

by:jafar78
ID: 19671499
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
 
LVL 51

Expert Comment

by:ahoffmann
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
0

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