Skip to main content
Question

Automatic Naming Example - Using API

  • June 6, 2018
  • 1 reply
  • 1 view

Forum|alt.badge.img+3

This is a script that gives a specific prefix for Location, adds Dept, then adds an identifier. It's fairly specific but the bash script for scraping the API response is good to use on anything you want to retrieve from JAMF. I currently have the identifier return a 3 digits to align to our naming conventions, but feel free to change it.

I apologize for my bash scripting format... not my primary language.

I set this to run on enrollment, but it can actually continually be run and sets the same name.

DEP Setup: After you associate computer to JAMF, I made several enrollment profiles and just filled in the department and location areas. This allows you to have the purchaser set everything before it even arrives. However, this is the only real manual setup you need to do and could be part of the ordering process. The Policy itself it set to run after enrollment.

1#!/bin/bash
2
3MACADDRESS=$(networksetup -getmacaddress en0 | awk '{ print $3 }')
4JSS=https://yourcompany.jamfcloud.com:443
5API_USER=api
6API_PASS=password
7
8
9## Get JAMF XML
10XML=$( curl -H "Accept:text/xml" -ksu $API_USER:$API_PASS $JSS/JSSResource/computers/macaddress/$MACADDRESS -X GET )
11
12##Find ID
13#String to search
14STRI="<id>"
15#Find Position in string
16IDPOS=${XML%%$STRI*}
17IDPOSNUM=${#IDPOS}
18#Find length of search variable
19STRLNGT=${#STRI}
20#Start after Tag
21IDFPOS=`echo "$STRLNGT + $IDPOSNUM" | bc`
22#Terminate string Length
23IDFPOST=`echo "$STRLNGT + $IDPOSNUM + 10" | bc`
24#Grab String and cut excess
25ID=`echo ${XML:$IDFPOS:$IDFPOST} | cut -d "<" -f 1`
26
27##Find Department
28#String to search
29STRD="<department>"
30#Find Position in string
31DPTPOS=${XML%%$STRD*}
32DPTPOSNUM=${#DPTPOS}
33#Find length of search variable
34STRLNGTD=${#STRD}
35#Start after Tag
36DPTFPOS=`echo "$STRLNGTD + $DPTPOSNUM" | bc`
37#terminate string length
38DPTFPOST=`echo "$STRLNGTD + $DPTPOSNUM + 10" | bc`
39#Grab string and cut excess
40DEPT=`echo ${XML:$DPTFPOS:$DPTFPOST} | cut -d "<" -f 1`
41
42##Find Location
43#String to search
44STRL="<building>"
45#Find Position in string
46LOCPOS=${XML%%$STRL*}
47LOCPOSNUM=${#LOCPOS}
48#Find length of search variable
49STRLNGTL=${#STRL}
50#Start after Tag
51LOCFPOS=`echo "$STRLNGTL + $LOCPOSNUM" | bc`
52#terminate string length
53LOCFPOST=`echo "$STRLNGTL + $LOCPOSNUM + 10" | bc`
54#Grab string and cut excess
55LOCATION=`echo ${XML:$LOCFPOS:$LOCFPOST} | cut -d "<" -f 1`
56
57
58#Find if it's a laptop
59IS_LAPTOP=`/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book"`
60#find ID length
61idlength=`echo ${#ID}`
62
63#Add Location
64if [ "$LOCATION" = "Home Office" ]; then
65 PREFIX=HO
66else
67 echo "Unknown Location"
68 exit 1
69fi
70
71#Determine Laptop or Not
72if [ "$IS_LAPTOP" != "" ]; then
73 PREFIX=$PREFIX"L"
74else
75 PREFIX=$PREFIX"D"
76fi
77
78#Sort Department Naming
79if [ "$DEPT" = "IT" ]; then
80 PREFIX=$PREFIX"777"
81elif [ "$DEPT" = "Marketing" ]; then
82 PREFIX=$PREFIX"666"
83elif [ "$DEPT" = "Web" ]; then
84 PREFIX=$PREFIX"555"
85elif [ "$DEPT" = "Private Label" ]; then
86 PREFIX=$PREFIX"444"
87else
88 PREFIX=$PREFIX"XXX"
89fi
90
91#format ID to 3 characters
92if [ $idlength -gt 3 ]; then
93 ID=echo ${ID: -3}
94 Name=$PREFIX$ID
95elif [ $idlength -lt 3 ]; then
96 ID="0"$ID
97 Name=$PREFIX$ID
98else
99 Name=$PREFIX$ID
100fi
101
102
103#set name to match
104/usr/sbin/scutil --set ComputerName "$Name"
105/usr/sbin/scutil --set LocalHostName "$Name"
106/usr/sbin/scutil --set HostName "$Name"
107
108#Flush naming cache
109dscacheutil -flushcache
110
111#Report to Jamf
112jamf recon
113
114exit 0

Hope that helps people with similar formats.

Edit: If your having issues with the API returns, I have it set to only move to 10 characters past where it sees the tag. You can adjust that by changing the 10 to whatever number you want. Technically I could put 100, but I wanted to run as efficient as possible.

1 reply

Forum|alt.badge.img+3

Updated for 10.5 This will now force XML as a response.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings