BladeSystem Forums Have Moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Populate /etc/hosts from nslookup of hostname

Super Advisor

Populate /etc/hosts from nslookup of hostname

Hi All,

I am thinking that I have seen a script on this forum which populates /etc/hosts from nslookup output. I am unable to find it.

Thanks,
Allan.
8 REPLIES
Esteemed Contributor

Re: Populate /etc/hosts from nslookup of hostname

Don't know about exisitng script,
but here's a start with no sanity checking:


#
###
### IP to Name ###
###
IP=$1
if [ "$IP" = "" ] ; then
echo "usage: $0 "
exit 1
fi

NAME=$(nslookup $IP | grep Name: | awk '{print $2}')

if [ "$NAME" = "" ] ; then
echo IP $IP not found >&2
exit 2
else
echo "$IP $NAME"
fi



You could this in a number of ways.

## ip_to_name 172.16.0.12
172.16.0.12 manny.fepu.com


## ip_to_name 172.16.0.12 >> /tmp/new_hosts_list



Say /tmp/iplist has list of ip addresses to convert

## ( > /tmp/new_hosts_list ; > /tmp/error
cat /tmp/iplist \
|while read IP
do
ip_to_name $IP >> /tmp/new_hosts_list 2>>/tmp/error
done
)


bv
"The lyf so short, the craft so long to lerne." - Chaucer
Acclaimed Contributor

Re: Populate /etc/hosts from nslookup of hostname

Hi Allan:

This would be simple enough:

# nslookup www.firefox.com|awk '{if (/^Name/) {N=$2;getline;A=$2;print A,N}}' >> /etc/hosts

This will handle multiple addresses, too, as for 'www.google.com'.

Regards!

...JRF...
Esteemed Contributor

Re: Populate /etc/hosts from nslookup of hostname

HAHA
I don't know why I assumed that the input was an IP address !?!?
Brain fart, I guess.

Better would be:

#
H=$1
if [ "$H" = "" ] ; then
echo "usage: $0 {host_name | IP_address}"
exit 1
fi

NAME=$(nslookup $H | grep Name: | awk '{print $2}')
IP=$(nslookup $H | grep Address: | awk '{print $2}')

if [ "$NAME" = "" -o "$IP" = "" ] ; then
echo Host/IP $H not found >&2
exit 2
else
echo "$IP $NAME"
fi



bv
"The lyf so short, the craft so long to lerne." - Chaucer
Super Advisor

Re: Populate /etc/hosts from nslookup of hostname

Thanks Guys,

Another thing would be what if the host is already there with the same IP address so I want to avoid populating the hosts file then.

and also delete the same hosts if the IP address changes for it, how can I tackle that so as to avoid having two or more entries for the same host even though the IPs are different.

Thanks,
Allan.
Acclaimed Contributor

Re: Populate /etc/hosts from nslookup of hostname

Hi (again) Allan:

> Another thing would be what if the host is already there with the same IP address so I want to avoid populating the hosts file then.

> and also delete the same hosts if the IP address changes for it, how can I tackle that so as to avoid having two or more entries for the same host even though the IPs are different.

Why do you want to maintain a local '/etc/hosts' file in the first place when DNS exists?

Further, if you would run 'nslookup' with a target like 'www.google.com' you would be rewarded with multiple addresses. What would you do in that case, since they are all valid and will be returned in a rotating order for each query.

Regards!

...JRF...
Highlighted
Super Advisor

Re: Populate /etc/hosts from nslookup of hostname

Hi JRF, one of the programs times out on dns resolution, so I want to use /etc/hosts file.

Thanks,
Allan.
Acclaimed Contributor

Re: Populate /etc/hosts from nslookup of hostname

Hi Allan:

> Hi JRF, one of the programs times out on dns resolution, so I want to use /etc/hosts file.

I would suggest you find the reason for that program's behavior rather than kludge up a local view of what rightly belongs in DNS. I guarantee that once you rely on a local hosts file, one day the IP address of a host will change and someone (you?) will wonder why the program can't reach its target.

Regards!

...JRF...
Acclaimed Contributor

Re: Populate /etc/hosts from nslookup of hostname

Allan:

Were you satisfied with the answers your received?

...JRF...