1826187 Members
2603 Online
109691 Solutions
New Discussion

Scripting

 
SOLVED
Go to solution
Aggy
Frequent Advisor

Scripting

Can someone help me with Scripting.
1)We want to move our entries from the /etc/hosts file to Windows DNS . We have got nearly 700 entries in the host file and only few exists in the Windows DNS.Without a script I will have to manually check if the entry is present in the DNS,if it is then delete from the host file and if it is not in DNS then add the entry on Windows Server and delete it from the /etc/hosts file.

I wanted to write a script which specifies any entries in the HOSTS file that are NOT present on the DNS server and the script must check both forward and reverse lookup
7 REPLIES 7
Peter Nikitka
Honored Contributor

Re: Scripting

Hi,

HP-UY provides a program 'host_to_named' - check its man page. It should be the starting point for your script, if it should be not possible to do all your stuff via that.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Ninad_1
Honored Contributor
Solution

Re: Scripting

Hi,

Please use the following script

#!/usr/bin/ksh
egrep -v "^#|^$" /etc/hosts | awk '{print $1,$2}' | while read ipaddr hostnm rest
do
nslookupip=$(nslookup $hostnm 2>/dev/null | awk '/Trying DNS/,/zzz/' | grep -i address | awk '{print $NF}')
if [[ -z $nslookupip ]]
then
echo "NO ENTRY in DNS for $hostnm" >> newhosts.log
else
if [[ "$ipaddr" = "$nslookupip" ]]
then
echo "VALID ENTRY for $hostnm in DNS" >> newhosts.log
fi
fi
done


which I have modified a bit for your case. I had suggested this for the following thread http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1036190

You can filter the entries not in DNS by
grep 'NO ENTRY' newhosts.log

Regards,
Ninad
spex
Honored Contributor

Re: Scripting

Aggy,

I modified Ninad's script to do reverse DNS, as well. I hope you don't mind, Ninad.

Here it is:

#!/usr/bin/ksh
cat /dev/null > newhosts_rev.log
ns=$(awk '/nameserver/ {print $2; exit}' /etc/resolv.conf)
egrep -v "^#|^$" /etc/hosts | awk '{print $1,$2}' | while read ipaddr hostnm rest
do
nslookupnm=$(nslookup $ipaddr $ns 2>/dev/null | \
awk '/Trying DNS/,/zzz/' | grep 'Name' | awk '{print $NF}')
echo $nslookupnm
if [[ -z $nslookupnm ]]
then
echo "NO REVERSE ENTRY for $ipaddr ($hostnm)" >> newhosts_rev.log
else
hostnm=$(echo $hostnm | cut -d. -f1)
nslookupnm=$(echo $nslookupnm | cut -d. -f1)
if [[ "$hostnm" = "$nslookupnm" ]]
then
echo "VALID REVERSE ENTRY for $ipaddr ($hostnm)" >> newhosts_rev.log
fi
fi
done

The nameserver is taken from /etc/resolv.conf and passed as an argument to nslookup. This prevents nslookup from looking at /etc/hosts for entries.

PCS
Aggy
Frequent Advisor

Re: Scripting

Thank you Ninad and Spex
Can you please suggest me a book for Korn Scripting.
James R. Ferguson
Acclaimed Contributor

Re: Scripting

Hi Aggy:

A free quick-start to shells can be found here:

http://docs.hp.com/en/B2355-90046/B2355-90046.pdf

I would urge you to use the Posix shell for HP-UX. This is the HP standard shell and *required* to be 'root's default shell. THe Posix shell is almost identical to the Korn shell, although the modern Korn shells do offer some nice features not yet present.

Regards!

...JRF...
spex
Honored Contributor

Re: Scripting

Aggy,

Hands down my favorite guide to scripting is "Shells: User's Guide: HP 9000 Computers", available at:

http://docs.hp.com/en/B2355-90046/

Korn is covered, along with others. I would urge you to use the POSIX shell, as it is the standard, and almost identical to ksh.

PCS
Aggy
Frequent Advisor

Re: Scripting

Thanks Everyone