-
Notifications
You must be signed in to change notification settings - Fork 2
/
Redeploy Jamf Management Framework
104 lines (85 loc) · 2.75 KB
/
Redeploy Jamf Management Framework
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# Redeploys the Jamf Management Framework
# for enrolled device
#
##################################################
# Variables -- edit as needed
# Jamf Pro API Credentials
jamfProAPIUsername=""
jamfProAPIPassword=""
jamfProURL=""
if [ -z $jamfProAPIUsername ]; then
echo "Please enter your Jamf Pro Username: "
read -r jamfProAPIUsername
fi
if [ -z $jamfProAPIPassword ]; then
echo "Please enter your Jamf Pro password for $jamfProAPIUsername: "
read -r -s jamfProAPIPassword
fi
if [ -z $jamfProURL ]; then
echo "Please enter your Jamf Pro URL (with no slash at the end): "
read -r jamfProURL
fi
# Token declarations
token=""
tokenExpirationEpoch="0"
# Jamf Pro Computer ID of computer you wish to redeploy the framework
id="343"
if [[ $id = "" ]]; then
read -p "Please enter the computer ID number that you would like to redeploy Jamf Framework: " id
fi
#
##################################################
# Functions -- do not edit below here
# Get a bearer token for Jamf Pro API Authentication
getBearerToken(){
# Encode credentials
encodedCredentials=$( printf "${jamfProAPIUsername}:${jamfProAPIPassword}" | /usr/bin/iconv -t ISO-8859-1 | /usr/bin/base64 -i - )
authToken=$(/usr/bin/curl -s -H "Authorization: Basic ${encodedCredentials}" "${jamfProURL}"/api/v1/auth/token -X POST)
token=$(/bin/echo "${authToken}" | /usr/bin/plutil -extract token raw -)
tokenExpiration=$(/bin/echo "${authToken}" | /usr/bin/plutil -extract expires raw - | /usr/bin/awk -F . '{print $1}')
tokenExpirationEpoch=$(/bin/date -j -f "%Y-%m-%dT%T" "${tokenExpiration}" +"%s")
}
checkTokenExpiration() {
nowEpochUTC=$(/bin/date -j -f "%Y-%m-%dT%T" "$(/bin/date -u +"%Y-%m-%dT%T")" +"%s")
if [[ tokenExpirationEpoch -gt nowEpochUTC ]]
then
/bin/echo "Token valid until the following epoch time: " "${tokenExpirationEpoch}"
else
/bin/echo "No valid token available, getting new token"
getBearerToken
fi
}
# Invalidate the token when done
invalidateToken(){
responseCode=$(/usr/bin/curl -w "%{http_code}" -H "Authorization: Bearer ${token}" ${jamfProURL}/api/v1/auth/invalidate-token -X POST -s -o /dev/null)
if [[ ${responseCode} == 204 ]]
then
/bin/echo "Token successfully invalidated"
token=""
tokenExpirationEpoch="0"
elif [[ ${responseCode} == 401 ]]
then
/bin/echo "Token already invalid"
else
/bin/echo "An unknown error occurred invalidating the token"
fi
}
redeployFramework(){
curl --request POST \
--url ${jamfProURL}/api/v1/jamf-management-framework/redeploy/${id} \
--header "Accept: application/json" \
--header "Authorization: Bearer ${token}"
exit 0
}
#
##################################################
# Script Work
#
#
# Calling all functions
checkTokenExpiration
redeployFramework
checkTokenExpiration
invalidateToken
exit 0