Background: we've got several hundred machines with IP addresses assigned via the DHCP server. All the machines have named entries in the DNS server. My goal was to write a script that would rename the computer with the DNS name defined on the DNS server. Basically, does a reverse lookup of it's IP address, and uses the resulting DNS name to label the computer. Script assumes that ethernet 0 is being used.

 

# This queries the DNS server to get the computer name.

ip=`ifconfig en0 | grep "inet "|awk '{print $2}'`
asset=`host $ip |awk '{print $5 $6}' |awk -F. '{print $1}'`
echo Updating various computer names.
scutil --set HostName $asset
scutil --set ComputerName $asset
scutil --set LocalHostName $asset