top of page
Search

Set Mac Hostname Using JAMF API

  • Writer: Noah Cunning
    Noah Cunning
  • Oct 27, 2020
  • 3 min read

This script uses a combination of information stored locally on the computer and information pulled from JAMF using the API to create a hostname that follows the convention I have setup for my network.


Currently I like to name my computers based on the site, the user the computer is assigned to, and the model of the computer. With my script if we had someone at the site "District Office", it is assigned to "NCUNNING", and the model is a Macbook Pro, my script will gather this information from the JAMF API and locally on the machine and make my hostname "DO-NCUNNING-MBP".


I am using the serial number of the computer to get the XML information of that computer from the JAMF API. The information gathered we will be using to make the hostname is building and assigned user.


The script starts by downloading the XML from the JAMF API using the serial number of the machine it is running on.


We then use the xmllint binary to read the XML and fetch the string that is held within <building></building>, I then translate that to be abbreviated and capitalized. Depending on your environment you may not need to use this. You can change the string that is being searched by changing where it says building in this part of the script "string(//building)".


Next we gather the assigned user and their username by using the xmllint command again. I gather the username and translate it to be all capital. I also remove the character "-".


After that I use the system profiler to get me the model of the Mac. I translate the model to all capitals, then remove all numbers and symbols. I then use SED to abbreviate the model. From MACBOOKPRO tp MBP.


Now we construct the hostname, we take the values from site, username, and model and combine them into one variable. Now that they are one variable I use them to set the hostname, computername, and localhostname.


At the end I echo the complete hostname after a couple line breaks for logging purposes.



#!/bin/bash
#Created by Noah Cunning. noahcunning.com

#Declare server variables
server="xxxx"       #Server address
username="xxxx"     #JSS username with API privileges
password="xxxx"     #Password for the JSS account


#Gets serialnumber for use in API call.
SN=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

#Get XML and store in variable for xmlint.
xml1=$(curl -X GET -u ${username}:${password} ${server}computers/serialnumber/$SN -H "accept: application/xml")

#Gets site tag from XML.
SITE1=$(echo "$xml1" | xmllint --xpath "string(//building)" -)
SITE2=$(echo $SITE1 | sed -- 's/^$/NONE/g; s/BES/B/g; s/DO/DO/g; s/EC/EC/g; s/MCMS/MC/g; s/OHES/OH/g; s/OPHS/HS/g; s/OPIS/IS/g; s/OPNS/NS/g; s/OVHS/OV/g; s/ROES/R/g; s/Technology/IT/g;')

#Gets username tag from XML.
USER1=$(echo "$xml1" | xmllint --xpath "string(//username)" -)
USER2=$(echo $USER1 | tr a-z A-Z)
USER3=$(echo $USER2 | sed -- 's/-//g')

#This gets the model identifier IE "MacBookPro16,1"
MODEL1=$(system_profiler SPHardwareDataType | awk '/Model Identifier/ {print $3}' | tr a-z A-Z)
#This removes numbers and commas from the model identifier
MODEL2=$(echo $MODEL1 | sed 's/[0-9]//g; s/,//g')
#I am making a prediction that Apple will release a Mac Mini Pro. 07/29/2020
MODEL3=$(echo $MODEL2 | sed -- 's/MACBOOKPRO/MBP/g; s/IMAC/IM/g; s/IMACPRO/IMP/g; s/MACMINI/MM/g; s/MACMINIPRO/MMP/g; s/MACPRO/MP/g; s/MACBOOKAIR/MBA/g; s/MACBOOK/MB/g; s/POWERMAC/PM/g')

##Final Hostname
HOSTNAME="$SITE2"-"$USER3"-"$MODEL3"

#This will set the hostname, computername, and localhostname to the string that is contained in the $HOSTNAME variable.
scutil --set HostName $HOSTNAME
scutil --set ComputerName $HOSTNAME
scutil --set LocalHostName $HOSTNAME

#This will update JAMF with the new hostname.
jamf recon
jamf policy

#Echo for policy log.
echo -e "\n\n\n$HOSTNAME"

 
 
 

Recent Posts

See All
Zoom Scripted Install

This script will automatically download the latest zoom version and install it. I use this script in a policy that run weekly for my Jamf...

 
 
 

Comments


©2020 PROUDLY CREATED BY NOAH CUNNING

bottom of page