-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prestage_Serial.py
63 lines (47 loc) · 1.94 KB
/
Prestage_Serial.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# Matthew Prins 2023
# https://github.com/MatthewPrins/Jamf/
# finds the prestage for a specific serial number via the API
# pip install requests if not already done
import json
import requests
from datetime import datetime
############################
# Editable variables
# Jamf credentials
username="xxxxxx"
password="xxxxxx"
jamfurl="https://xxxxxx.jamfcloud.com"
from credentials import *
#Serial number for device looking for
serial="DY5DD13SJ1WF"
#True for computers, False for mobile devices
lookingforcomputer = False
############################
# definition for fetching API token
def fetchtoken():
response = requests.post(url=jamfurl + "/api/v1/auth/token", headers={'Accept': 'application/json'}, auth=(username, password))
print("new token fetched\n")
return response.json()['token'], datetime.strptime(response.json()['expires'], '%Y-%m-%dT%H:%M:%S.%fZ')
# definition for invalidating API token
def invalidatetoken():
response = requests.post(url=jamfurl + "/api/v1/auth/invalidate-token", headers={'Authorization': 'Bearer ' + token})
############################
# fetch API token
token,tokenexpiration = fetchtoken()
if lookingforcomputer:
apitext = "computer-prestages/"
else:
apitext = "mobile-device-prestages/"
# import data from API as a dictionary
response = requests.get(jamfurl + "/api/v2/" + apitext + "scope", headers={'Accept': 'application/json','Authorization': 'Bearer ' + token})
serialdictionary = json.loads(response.text)['serialsByPrestageId']
# get prestage name for that serial number
try:
response = requests.get(jamfurl + "/api/v2/" + apitext + serialdictionary[serial], headers={'Accept': 'application/json','Authorization': 'Bearer ' + token})
prestagedictionary = json.loads(response.text)
print(serial + ": " + prestagedictionary['displayName'] + " (" + serialdictionary[serial] + ")")
# error message if not found
except:
print("serial " + serial + " not found in any prestage")
exit()