2 shell scripts to maintain standard naming conventions

So, you have some Macintosh computers at your work and you have a standard naming convention.  Something that needs to be in place because of how policies are ran, say by a computer name list or a group of computer names.   If a user changes it, it could break something you have in place.

So, I came up with a sort of clever way of maintaining the computer name at all times.  My method does have a few requirements though.  Here is a list of what you must do and have in place for this to really work:

  1. The name must be set and correct before you run the first script
  2. You must have some sort of task server, Casper Suite, or equivalent to make it run the second script to check the name.
  3. The ability to create a policy that runs the second script daily, or whenever

Alternatively, you could run both scripts locally via launchd on a scheduled basis, but that would take up more resources than having it set up as a task of network policy.  Though, it would be very possible.

So, I recommend running the first script as a post imaging script.  If you have in your imaging process a step for setting the computer name to the correct name and naming convention, then the first script would be ideally ran at that time.  Here is the script:

 

 

#!/bin/bash
 
###########################
#This is part 1 of a 2 part script for the Casper Suite to
#help maintain proper naming conventions
#
#by
#Thomas Larkin <a href="http://tlarkin.com
#
#USE" title="http://tlarkin.com
#
#USE">http://tlarkin.com
#
#USE</a> AT OWN RISK
###########################
 
#first we need to set the computer name to a local file
#in this case the file will be called computername.conf
#this file will be located in /Library/Receipts
#this would be best run after a computer is imaged
 
checkfile=`/bin/ls /Library/Receipts | grep computername.conf -c`
 
#now check if it exists if not create it
 
if [ $checkfile -eq 1 ]
 
	then /bin/echo "$checkfile exists already"
	exit 1
 
fi
 
#now we need to set the current computer's name to this file
#if your JSS database is correct and this is ran post image, it shoudl always match the 
#name in the JSS for this particular machine.
 
/usr/sbin/networksetup -getcomputername &gt; /Library/Receipts/computername.conf
 
 
exit 0

 

As you can see that script just creates a file in /Library/Receipts called computername.conf and then writes the computer name to it.  Then applies ownership and persmission to root:admin and disallows access from everyone else.  Really, you could easily create this file from the GUI, but if you need to push this out remotely, the script is the way to do it.

Once you get the computername.conf file created you will want to run the second script to check the data of that file and make sure the name is correct.  Here is the second script:

 

 

#!/bin/bash
 
########################################
#This is part 2 of a 2 part script to help maintain standard naming conventions
#in conjunction with the Casper Suite
#
# by
# Thomas Larkin <a href="http://tlarkin.com
#
#USE" title="http://tlarkin.com
#
#USE">http://tlarkin.com
#
#USE</a> AT YOUR OWN RISK
#########################################
 
#this should be set to run once a day, or whatever frequency you want
#it will compare what is stored in /Library/Receipts/computername.conf
#to the current computer name, and if they do not match it will use the one
#that lives in /Library/Receipts/computername.conf
 
current_computername=`/usr/sbin/networksetup -getcomputername`
 
set_name=`/bin/cat /Library/Receipts/computername.conf`
 
#now compare both strings 
 
if [[ $current_computername == $set_name ]]
 
	then echo "All looks good, computer name checks good"
 
	else /usr/sbin/networksetup -setcomputername $set_name
 
fi
 
 
exit 0

 

This script should be ran from a task server, Casper policy, or other third party utility that will run it on an interval.  That way you can have it constantly check itself for the proper naming convention.

I am sure there are better ways to do this, and if you know one plase feel free to drop me a line and give me your ideas.

Thanks for reading,

Tom

 

 

Your rating: None Average: 5 (1 vote)

Comments

There seems to be some sort

There seems to be some sort of bug with the GeSHi filter where the last line of my first script is not showing > like it should.  Instead it is rendering it as the literal variable.  I am looking into getting this fixed.