Naming standard on mac

Captainamerica
Contributor II

I guess I am not the only one having issues how to setup naming standards for Mac that follow company rules.

For us we need simply having a country code as - fx US-%mac address - so would like US-E34E3E4TE5

But what is the best way to set this up as it is not a static named depending on where users are located. If users have manually to choose would not be a problem, as long it will happen as one of the first steps in the enrollment process -- and also for Dep of course

11 REPLIES 11

stevewood
Honored Contributor II
Honored Contributor II

@Captainamerica You can set a script to run as a Before script, or add to any existing script, that runs on enrollment or when you are provisioning a machine. I use several scripts during provisioning and one of those is a naming script. Our computer names are based off of several different criteria like country, Hyperion code, agency (business name), and a portion of the computer serial number. We currently use cocoaDialog to grab some of the info to be able to build the computer name on the fly. However for your exercise, as long as the country is static and all that is dynamic is the MAC address, this little snippet should work. I'm making an assumption that en0 will suffice to pull the MAC from:

#!/bin/bash
macAdd=`ifconfig en0 | awk '/ether/{print $2}' | sed s/://g`
compName='US-${macAdd}'

/usr/local/bin/jamf setComputerName -name {$compName}
/usr/local/bin/jamf recon
exit 0

If you want the MAC from a different interface, you can change en0 in the macAdd variable.

That will change the name on the computer and then run an inventory update to make sure the name change is reflected in the Jamf Pro server.

If you would like to see how we handle it, I have a template script up from my JNUC 2018 talk: JNUC 2018

ryan_ball
Valued Contributor

This will get you the device's country code:

countryCode=$(defaults read /Library/Preferences/.GlobalPreferences.plist "com.apple.AppleModemSettingTool.LastCountryCode")
echo "$countryCode"

Captainamerica
Contributor II

Think I described it Wrongly then. The country code is dynamic depending on where users are Located. So users have to choose country and then the mac should be named like fox uk-Mac Address

Captainamerica
Contributor II

Anyone has some input on this naming that has to be dynamic based on where user is based ?

donmontalvo
Esteemed Contributor III

Something like this?

#!/bin/bash

# Create folder
/bin/mkdir -p /Library/COMPANY
/bin/chmod -R 755 /Library/COMPANY
macAddress=$( ifconfig en0 | awk '/ether/{print $2}' | sed s/://g )
nameFile=/Library/COMPANY/computerName.txt

# Prompt for Country
countryValue=`/usr/bin/osascript <<EOT
tell application "System Events"
    with timeout of 9999 seconds
    activate
    set theCountry to {"US", "UK", "CH"}
    set selectedCountry to {choose from list theCountry with prompt "Select Country"}
    end timeout
end tell
EOT`
echo "$countryValue-$macAddress" > $nameFile
nameValue=$( cat $nameFile )

# Set ComputerName|LocalHostName|HostName
scutil --set ComputerName $nameValue
scutil --set LocalHostName $nameValue
scutil --set HostName $nameValue

exit 0

Since you're on Jamf Pro 10.9, if you set a policy that runs this script, triggered by Enrollment Complete, there shouldn't be a race condition, in case there are any policies depending on the ComputerName being defined (for example binding to AD).

You'd have to add all the two character country abbreviations to the set theCountry to line of course.

--
https://donmontalvo.com

Captainamerica
Contributor II

@donmontalvo Thank for your reply and actually it seems to work. We do not bind them to AD, so that is not the issue.
If we want to use the serial number instead of Mac adress, is it just adding Serial$ instead ?

JMR
New Contributor II

You could add something like this to obtain the serial number:

serialNumber="$(system_profiler SPHardwareDataType | grep Serial | cut -f2 -d : | cut -d " " -f2)"

Then reference that in the script instead of macAddress.

donmontalvo
Esteemed Contributor III

ioreg is a little faster:

$ time ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}'
XXXXXXXXXXXXX

real    0m0.005s
user    0m0.003s
sys 0m0.004s

...than parsing through output of system_profiler

$ time system_profiler SPHardwareDataType | grep Serial |  cut -f2 -d : | cut -d " " -f2
XXXXXXXXXXXXX

real    0m0.222s
user    0m0.072s
sys 0m0.070s

So would look like this using Serial Number:

#!/bin/bash

# Create folder
/bin/mkdir -p /Library/COMPANY
/bin/chmod -R 755 /Library/COMPANY
serialNumber=$( ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}' )
nameFile=/Library/COMPANY/computerName.txt

# Prompt for Country
countryValue=`/usr/bin/osascript <<EOT
tell application "System Events"
    with timeout of 9999 seconds
    activate
    set theCountry to {"US", "UK", "CH"}
    set selectedCountry to {choose from list theCountry with prompt "Select Country"}
    end timeout
end tell
EOT`
echo "$countryValue-$serialNumber" > $nameFile
nameValue=$( cat $nameFile )

# Set ComputerName|LocalHostName|HostName
scutil --set ComputerName $nameValue
scutil --set LocalHostName $nameValue
scutil --set HostName $nameValue

exit 0
--
https://donmontalvo.com

JMR
New Contributor II

@donmontalvo Some say ioreg is too fast. No really thank you that is very helpful - always nice to save a little time running a script.

donmontalvo
Esteemed Contributor III

LOL, even faster if we eliminate the output to file part:

#!/bin/bash

# Create folder
/bin/mkdir -p /Library/COMPANY
/bin/chmod -R 755 /Library/COMPANY
serialNumber=$( ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}' )

# Prompt for Country
countryValue=`/usr/bin/osascript <<EOT
tell application "System Events"
    with timeout of 9999 seconds
    activate
    set theCountry to {"US", "UK", "CH"}
    set selectedCountry to {choose from list theCountry with prompt "Select Country"}
    end timeout
end tell
EOT`

# Set ComputerName|LocalHostName|HostName
scutil --set ComputerName  "$countryValue-$serialNumber"
scutil --set LocalHostName  "$countryValue-$serialNumber"
scutil --set HostName  "$countryValue-$serialNumber"

exit 0

Doesn't accommodate slow techs...hehe...

--
https://donmontalvo.com

Captainamerica
Contributor II

@donmontalvo I hope you can help me with a little correction.

Right now the computer name is US-XXXXXX serial,
But it should be USMAC-XXXXXX (so all country code should have XXMac-XXXXXX serial)

I tried to add some prefix, but I cannot get it working