Skip to content

Instantly share code, notes, and snippets.

@nmcspadden
Created June 4, 2014 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmcspadden/613e8a04b60515fe43a2 to your computer and use it in GitHub Desktop.
Save nmcspadden/613e8a04b60515fe43a2 to your computer and use it in GitHub Desktop.
This is for historical purposes, and likely can be done better via the new JSS 9.x API
#!/usr/bin/python
import sys
import csv
import subprocess
try:
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')
f.write(output)
f.close()
#
# 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("<mobile_device>\n")
f.write("\t<location>\n")
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>%s.%s.%s@sacredsf.org</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)
f.write("\t</location>\n")
f.write("</mobile_device>\n")
f.close()
# 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