[CentOS] Finding DHCP IP of guest system

Sat Jul 17 05:21:28 UTC 2010
Jay Leafey <jay.leafey at mindless.com>

JohnS wrote:
> Awsome but a Day Late and a Dollar Short && Care to share that shell
> script please.

OK, but I warned you, it's a kludge.

> #!/bin/bash
> #
> #  Get a list of the hosts on the local network via nmap -sP and check 
> #  them against the ethers file to retrieve the host name, if any.  
> #  Check DNS to see if the DNS entries match it in the local domain and, 
> #  if not, make the necessary changes.
> #
> #  $Id$
> #  Jay Leafey - 10/29/2009
> #
> 
> TEST=0
> test $# -gt 0 && TEST=1
> 
> NSUPDATES=$( mktemp -t dynamic_dns.XXXXXXXXXX )
> ME=$( hostname -f )
> 
> echo "server localhost" > ${NSUPDATES}
> 
> nmap -sP 192.168.1.0/24 | \
> while read f1 f2 f3 f4 f5
> do
>     if [ "${f1}" == "Host" ]
>     then
>                 if [ "${f2}" == "${ME}" ]
>                 then
>                         continue
>                 fi
>                 read m1 m2 m3 m4 m5
>                 MYIP=""
>                 if [ "${f2%.*}" == "192.168.1" ]
>                 then
>                         MYIP=${f2}
>                 else
>                         MYIP=$( echo "${f3}" | sed 's/[\(\)]//g' )
>                 fi
>                 MYMAC=${m3}
>                 MYHOST=$( grep -i "^${MYMAC}" /etc/ethers | awk "{ print \$2 }" | tr A-Z a-z)
>                 #~ echo "${MYMAC} ${MYIP} ${MYHOST}"
> 
>                 if [ "${MYHOST}" ]
>                 then
>                 #~ Set the "forward" DNS entry
>                         DNSIP=$( host ${MYHOST} 2>/dev/null | awk '/ has address / { print $NF}' )
>                         if [ -z "${DNSIP}" ]
>                         then
>                                 echo -e "update add ${MYHOST}.local 2400 IN A ${MYIP}\n" >> ${NSUPDATES}
>                         elif [ "${MYIP}" != "${DNSIP}" ]
>                         then
>                                 echo "update delete ${MYHOST}.local IN A ${DNSIP}" >> ${NSUPDATES}
>                                 echo -e "update add ${MYHOST}.local 240 IN A ${MYIP}\n" >> ${NSUPDATES}
>                         fi
>                         #~ Set the "reverse" DNS entry
>                         DNSRR=$( host ${MYIP} | awk '/ domain name pointer / { print $1 }' )
>                         DNSPTR=$( host ${MYIP} | awk '/ domain name pointer / { print $NF }' )
>                         if [ -z "${DNSPTR}" ]
>                         then
>                                 echo -e "update add ${MYIP##*.}.1.168.192.in-addr.arpa 2400 IN PTR ${MYHOST}.local.\n" >> ${NSUPDATES}
>                         elif [ "${DNSPTR}" != "${MYHOST}.local." ]
>                         then
>                                 echo "update delete ${DNSRR} IN PTR" >> ${NSUPDATES}
>                                 echo -e "update add ${DNSRR} 2400 IN PTR ${MYHOST}.local.\n" >> ${NSUPDATES}
>                         fi
>                 fi
>         fi
> done 
> 
> if [ ${TEST} -gt 0 ]
> then
>     cat ${NSUPDATES}
>     exit
> fi
> 
> if [ $( wc -l ${NSUPDATES} ) -gt 1 ]
> then
> #        cat ${NSUPDATES}
>         nsupdate ${NSUPDATES}
>         if [ $? -ne 0 ]
>         then
>                 echo "nsupdate failed:"
>                 cat ${NSUPDATES}
>         fi
> fi
> 
> rm -f ${NSUPDATES}
> 
> exit

The code makes a LOT of assumptions that may only be valid in my home 
network, but perhaps the ideas will be useful.  I have considered 
rewriting this in Perl, but it works and I really need the time for 
other projects.

> && Why you scrub the MACS?

Sheer paranoia and long-standing habit.

Enjoy!
-- 
Jay Leafey - jay.leafey at mindless.com
Memphis, TN
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3274 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.centos.org/pipermail/centos/attachments/20100717/78402e85/attachment-0005.bin>