-
Notifications
You must be signed in to change notification settings - Fork 1
/
instant_checkin_cli.py
165 lines (139 loc) · 5.32 KB
/
instant_checkin_cli.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import requests
from unopass import unopass as secret
try:
print("\n\033[93mRetrieving Jamf secrets via unopass...\033[0m")
# 1password variables, {vault}, {item_name}, {field_name}
username = secret.unopass("personal", "hyper_jamf", "username")
password = secret.unopass("personal", "hyper_jamf", "credential")
jss = secret.unopass("personal", "hyper_Jamf", "url")
except Exception as e:
print(f"\n\033[91mError\033[0m: {e}")
exit(1)
def getBearerToken() -> str:
url = f"{jss}/api/v1/auth/token"
headers = {"Accept": "application/json"}
response = requests.post(url, auth=(username, password), headers=headers)
return response.json()["token"]
def invalidateToken(token: str) -> str:
url = f"{jss}/api/v1/auth/invalidate-token"
headers = {"Authorization": f"Bearer {token}"}
response = requests.post(url, headers=headers)
if response.status_code == 204:
print("\n\033[92mJamf token invalidated OK\033[0m")
elif response.status_code == 401:
print("\nJamf token already invalidated")
else:
print("\nError invalidating Jamf token")
def banner():
print(
"\n\033[92mWelcome to the instant-checkin CLI"
"\033[0m\n\n-------------------------------------"
)
def user_input() -> str:
while True:
print("\nEnter the employee username: Format amado.tejada\n")
username = input()
if "." in username:
return username
else:
print(
f"\n\033[91mError\033[0m: {username} is invalid | Format amado.tejada"
)
def update_room(chosen: str, token: str) -> None:
name = chosen.split(":")[1]
ids = chosen.split(":")[0]
headers = {"Authorization": f"Bearer {token}"}
data = "<computer><location><room>check-in</room></location></computer>"
url = f"{jss}/JSSResource/computers/id/{ids}"
resp = requests.put(url, data=data, headers=headers)
try:
if resp.status_code == 201:
print(f"\n\033[92mSuccess\033[0m: {ids}:{name} 🎉")
elif resp.status_code == 409:
print(f"\n\033[93mSkipping\033[0m: Jamf duplicate serial# for {ids}:{name}")
else:
print(resp.status_code)
print(f"\n\033[91mError\033[0m: {ids}:{name} was not updated")
except Exception as e:
print(f"\n\033[91mError\033[0m: {e}")
def main() -> None:
banner()
token = getBearerToken()
username = user_input()
computers, usernames = [], []
results, used = [], []
usernames = {username, username.replace(".", "")}
usernames.add(f"{username}@DOMAIN.com")
usernames.add(username.replace(".", "") + "@DOMAIN.com")
print(usernames)
for u in usernames:
headers = {"Accept": "application/json", "Authorization": f"Bearer {token}"}
url = f"{jss}/JSSResource/computers/match/{u}"
response = requests.get(url, headers=headers)
data = response.json()
if data != []:
computers.extend(iter(data["computers"]))
unique = [x for x in computers if x not in used and (used.append(x) or True)]
for c in unique:
if "ready" in c["room"].lower():
results.append(f"{c['id']}:{c['name']}")
else:
print(
f"\n\033[91mWarning: Not ready for Instant Check-in - deploy policy\033[0m "
f"\nID: {c['id']}, Laptop: {c['name']}, Serial: {c['serial_number']} "
f"\nPolicy: {jss}/policies.html?id=157\nLaptop: {jss}/computers.html?id={c['id']} 💻"
)
if results:
jamf_results(results, username, token)
else:
print(f"\n\033[91mError\033[0m: No ready computers found for {username}")
invalidateToken(token)
exit(1)
def jamf_results(results, username, token):
sresults = set(results)
print(
f"\n-------------------------------------\n"
f"\n\033[92mJamf Laptop Lookup Results ({len(sresults)}):\033[0m {username}\n"
)
for ids, name in enumerate(sorted(sresults), start=1):
print(f"{ids}, Laptop {name}")
sorted_results = sorted(sresults)
if len(sorted_results) > 1:
print(
"\nEnter a number from above or type "
"\033[92mALL\033[0m to check-in all laptops:\n"
)
else:
print(
"\nEnter the number from above to check-in the laptop:\n"
)
while True:
chosen = input()
if chosen.upper() == "ALL":
for c in sorted_results:
update_room(c, token)
break
elif chosen.isdigit():
if int(chosen) <= len(sorted_results) and int(chosen) > 0:
update_room(sorted_results[int(chosen) - 1], token)
break
else:
print(
f"\n\033[91mError\033[0m: {chosen} is not a valid option, try again below\n"
)
else:
print(
f"\n\033[91mError\033[0m: {chosen} is not a valid option, try again below\n"
)
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"\n\033[91mError\033[0m: {e}")
secret.signout(deauthorize=True)
exit(1)
except KeyboardInterrupt:
print("\n\n\033[91mExiting...\033[0m")
invalidateToken(getBearerToken())
secret.signout(deauthorize=True)
exit(1)