Skip to main content

This worklet can be run against Mac Hosts to standardize hostnames. This is valuable to our org in that we have a hostname standard of username-macbookModel that helps us identify the machine. You can change whatever values you’d like to accomodate your org.



Evaluation Code:



pro="-MBPro"

air="-MBAir"

emp=$(stat -f%Su /dev/console)



mbpro="${emp}${pro}"

mbair="${emp}${air}"



if f $HOSTNAME = "${mbpro}" ]] || | $HOSTNAME = "${mbair}" ]]; then

exit 0

else

exit 1

fi



Remediation code:



#!/bin/bash

# The script will identify the current logged in user and machine type and it will

# run as root and will set hostname to: user-computer type based on the information returned



pro="-MBPro"

air="-MBAir"

model=$(sysctl hw.model | sed 's/s0-9,]//g' | awk '/^/a-zA-Z]/ {print $2}')

emp=$(stat -f%Su /dev/console)



case "${model}" in

"MacBookPro" )

sudo scutil --set ComputerName "${emp}${pro}"

sudo scutil --set LocalHostName "${emp}${pro}"

sudo scutil --set HostName "${emp}${pro}"

sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string "${emp}${pro}"



;;

"MacBookAir" )

sudo scutil --set ComputerName "${emp}${air}"

sudo scutil --set LocalHostName "${emp}${air}"

sudo scutil --set HostName "${emp}${air}"

sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string "${emp}${air}"

esac

This is great, Mat!



I modified the script to achieve a different goal:



Some customers have an MDM solution that sets the ComputerName automatically upon endpoint provisioning. This script will check wether the LocalHostName or HostName do not match the ComputerName and will true them up to the ComputerName if there is a discrepancy.



Evaluation:



#!/bin/bash



#Get the ComputerName, LocalHostName, HostName values

enforced_name=`sudo scutil --get ComputerName`

localhostname=`sudo scutil --get LocalHostName`

hostname=`sudo scutil --get HostName`



#Check that LocalHostName and HostName are set to the same value as ComputerName

#If they match, exit gracefully; if they don't match, set the exit bit for remediation

if [[ "$localhostname" == "$enforced_name" && "$hostname" == "$enforced_name" ]]; then

exit 0

else

exit 1

fi



Remediation:



#!/bin/bash



#Get the ComputerName

enforced_name=`sudo scutil --get ComputerName`

#MacOS will not accept spaces in LocalHostName, so we check for spaces in the ComputerName and IFF they exist, replace them with dashes

refactored_name=$(sed 's/ /-/g' <<< "$enforced_name")



#Set the LocalHostName, HostName and NetBIOS variables to match the ComputerName

sudo scutil --set LocalHostName $refactored_name

sudo scutil --set HostName $refactored_name

sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string "$refactored_name"



>#Echo the results into the logs. Everything below this line is optional and can be commented out or removed.

lhostname=`sudo scutil --get LocalHostName`

hostname=`sudo scutil --get HostName`

echo "LocalHostName has been set to: $lhostname"

echo "HostName has been set to: $hostname"

Nice, this is a great adaptation!


Reply