Here is the part of my script that does the DNS reverse name lookup and sets the computer name to what is registered in our DHCP servers. For this to work, I first add the ethernet hardware address of all of my macs to our DHCP registry so that they get a preferred IP address which is assigned to a registered unique computer name. The network team here has a web form I can use to add macs one at a time or, if I need them to, they can parse a list of ethernet hardware addresses and associated names. I'm blessed to have such great support from our network team!
Anyway - here's the script snippet in case it helps someone else ... you'll need to copy this into a real script for it to work properly.
# Set the computer name to what it is in our DNS servers with a reverse
# name lookup on the IP address and trim the result to just the hostname
this_ip=$(ifconfig en0 | grep 'inet ' | grep -v '127.0.0.1' | awk '{ print $2}')
this_host=$(nslookup $this_ip|grep "name ="|sed 's/^.*name = //'| sed '$s/.$//')
this_name=${this_host%.williams.edu}
systemsetup -setcomputername $this_name
scutil --set ComputerName $this_name
scutil --set HostName $this_name
scutil --set LocalHostName $this_name
echo " ------- End computer rename"
# example of something you can do with predictable computer names that are set correctly
# determine if this is a lectern mac and change the desktop background accordingly
if [[ $this_name == *"-mac" ]]; then
echo " ------- Detected lectern mac, changing desktop background ... at: $(date) "
cp /Library/Application Support/Williams/DeskBackground/desklec.jpg /Library/Application Support/Williams/DeskBackground/deskback.jpg
fi
I included at the end an example of another part of the script where I change the deaktop background based on the predictable computer name. All my "lectern" macs - those macs attached to classroom projection systems - have their computer name end with "-mac". Therefore, I can use that to change the desktop background from the general lab image to the one for lecterns that gives very specific instructions and phone numbers for the classroom support team.
Anyway - hope it helps someone.