Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This is for historical purposes, and likely can be done better via the new JSS 9.x API
import sys
import csv
import subprocess
import xml.etree.cElementTree as ElementTree
except ImportError:
import xml.etree.ElementTree as ElementTree
## 1) Download allMobileDevices.xml from REST API
## 2) Parse XML for serial numbers/device ID
## 3) Parse Master500 CSV and mobileDeviceReport CSV
## 4) When a matching serial is found, create a temp XML with the relevant data
## 5) Use matching device ID to upload the correct XML for the correct device
username = ""
password = ""
jssserver = ""
# Step 1: Download allMobileDevices.xml from REST API
# curl -v -k -u $jssAPIUsername:$jssAPIPassword $jssAddress/JSSResource/mobiledevices -X GET > /tmp/allMobileDevices.xml
cmd = ['/usr/bin/curl','-v','-k','-u',username+':'+password,jssserver+'/JSSResource/mobiledevices','-X','GET']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errors) = proc.communicate() #proc.returncode has the result of this
f = open('allMobileDevices.xml', 'wb')
# Step 2: Parse the XML file and build a dict of serial : device id.
# Build the lookup dict for allMobileDevices
tree = ElementTree.parse('allMobileDevices.xml')
root = tree.getroot()
alldevices = dict()
devicelist = tree.findall('mobile_device')
# devicelist[0][2].text is the first device's serial number
# devicelist[0][0].text is the first device's ID number
# Step 3: Parse the Master 500 CSV and build a dict of serial : row of info from CSV
# The dict contains a key that is the serial number, and the value is the rest of the row
# Build the lookup dict for the purchased iPads
purchased_ipads = dict()
with open('Master_500.csv', 'rb') as ipadfile:
ipadlist = csv.reader(ipadfile, delimiter=',')
for ipadrow in ipadlist:
purchased_ipads[ipadrow[0]] = ipadrow
# Step 4: Match a serial from the 500 CSV to the list of all found devices, and then
# create a small blob of XML to be used for PUTting back to the JSS
# This section needs to be changed to rely on the allMobileDevices.xml instead of the CSV
for device in devicelist:
if purchased_ipads.get(device[2].text, None):
firstname = purchased_ipads.get(device[2].text, None)[7]
lastname = purchased_ipads.get(device[2].text, None)[6]
grade = purchased_ipads.get(device[2].text, None)[5]
dept = purchased_ipads.get(device[2].text, None)[4]
# Step 5: Upload temporary XML file to curl PUT
f = open('deviceFinal.xml', 'wb')
f.write( "\t\t<username>%s.%s.%s</username>\n" % (firstname.lower(), lastname.lower(), grade))
f.write( "\t\t<username>%s.%s.%s</username>\n" % (firstname.lower(), lastname.lower(), grade[2:]))
f.write("\t\t<real_name>%s %s</real_name>\n" % (firstname, lastname))
f.write("\t\t<email_address></email_address>\n" % (firstname.lower(), lastname.lower(), grade[2:]))
f.write("\t\t<position>student-%s</position>\n" % grade)
f.write("\t\t<department>%s</department>\n" % dept)
# Now curl it up
# id = device[0].text
# curl -k -v -u $jssAPIUsername:$jssAPIPassword $jssAddress/JSSResource/mobiledevices/id/$currentDeviceID -X PUT -T /tmp/deviceFinal.xml
cmd = ['/usr/bin/curl','-v','-k','-u',username+':'+password,jssserver+'/JSSResource/mobiledevices/id/'+device[0].text,'-X','PUT','-T','deviceFinal.xml']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errors) = proc.communicate()
print output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.