Mac Rename Script - Check to see if name is already in use

WindowsWarrior
New Contributor II

Currently, we're using the below script that works with renaming our machines and randomising the machine's name.

#!/bin/bash

num="$((RANDOM % 9999))"
size=${#num}

toadd="$((6 - $size))"

for (( i=1; i<=toadd; i++))
do
    num=0$num
done

compname=PREFIX-$num

if [ ${compname} ];
then       
    sudo scutil --set HostName $compname
    sudo scutil --set ComputerName $compname
    sudo scutil --set LocalHostName $compname
fi

echo $compname

 

Attempting to further improve this so machines do not end up with a duplicated name (no matter how small a chance!) there is.

 

Is there a way to do an inventory check for all machine names on Jamf Pro and then either a) add those numbers to an exclusion list so they're never chosen, or b) re-roll the randomiser section, after checking, if the name already exists?

 

How would the above be edited / shortened to make this a thing?
What possible improvements could be made to the above as well?

 

Any help is greatly appreciated :) !

7 REPLIES 7

jcarr
Release Candidate Programs Tester

The MAC address is a unique value.  You could use the Execute Command option in 'Files and Processes' to send this:

 

/usr/local/bin/jamf setComputerName -name "Mac-$(ifconfig en0 | awk '/ether/{print $2}' | tr -d :’)"

jcarr
Release Candidate Programs Tester

Serial number:

 

/usr/local/bin/jamf setComputerName -name "Mac-$(system_profiler SPHardwareDataType | awk '/Serial/ {print $NF}')"

 

or UDID:

 

/usr/local/bin/jamf setComputerName -name "Mac-$(ioreg -d2 -c IOPlatformExpertDevice | awk -F\" '/IOPlatformUUID/{print $(NF-1)}')"

 

would also work.

WindowsWarrior
New Contributor II

Thanks for your replies @jcarr !!

Though I need to stick to the format of:

PREFIX-00<RAND_FOUR_INT>
I was just wanting to know if there was a way to see if the name has already been used, and if so, randomise and check again.

it did occur to me to use the serial numbers, as they’d be unique per machine…. But the company wants the format above.

 

is there any thoughts / ideas / suggestions on how to accommodate this?

jcarr
Release Candidate Programs Tester

Do you have an internal web server where you could host a .csv file?  If so, set up a .csv with serial number in one column and a unique four digit code in the other.

 

Then set up a script to curl down the file, grep the file for the serial and read out the second field to a variable. Then set the name to “Prefix-“$code

 

Set up a policy to run the script every week, and if you have to update the file, just flush the history on the policy so it runs on every device at next check in. 

WindowsWarrior
New Contributor II

Unfortunately, no internal webservers, at least nothing relating to JAMF.
We seem to be using the cloud version.

 

otherwise, other web servers, we would not have access to. 

jcarr
Release Candidate Programs Tester

Well, that means an additional step, and makes keeping the data file up to date a little more difficult, but not impossible.  You could imbed the data into an array in the script, or build a package that installs the data file into /private/tmp and then run the naming script either as a part of the policy, or as a post install script in the package.

WindowsWarrior
New Contributor II

Would prefer the array in the script and would need to make sure it does a check to make sure the items within the array reflect actual names available as well. 
As the names could be changed/removed. 

if there’s a better platform to talk about this, somewhere I could set up a call to have you walk me through it… would that be possible?