forked from jamf/API_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddComputers_Static_API.py
46 lines (36 loc) · 1.11 KB
/
AddComputers_Static_API.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python
import StringIO
import csv
from urllib2 import urlopen, URLError, HTTPError, Request
import base64
#path to CSV file
csvfile='/Users/Shared/computers.csv'
#Use full REST url for JSS URL
#Example: jssurl='https://jss.company.com:8443/JSSResource/computergroups/id/7'
jssurl=''
jssuser=''
jsspass=''
#open the csv
with open (csvfile, 'r') as myfile:
data=myfile.read().replace('\n', '')
#create a list from the csv
f = StringIO.StringIO(data)
reader = csv.reader(f, delimiter=',')
#format the data from list into a xml
csvdata = '<computer_group><computers>\n'
for row in reader:
for w in row:
csvdata += '<computer><name>' + w + '</name></computer>\n'
csvdata += '</computers></computer_group>'
#print out contents as an error check
print('contents of data:\n' + csvdata)
#submit to the JSS API
req = Request(jssurl, data=csvdata)
req.add_header('Content-Type', 'text/xml')
req.get_method = lambda: 'PUT'
base64string = base64.encodestring(jssuser+':'+jsspass)[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
xmldata = urlopen(req)
#print result
print(xmldata)