1#!/bin/bash
2
3###########################################################################################################
4################## IMPORTANT - ENTER JSS INFORMATION IN "apiQuery" FUNCTION BEFORE USING ##################
5###########################################################################################################
6
7# Set field seperator to newline
8IFS=$'
9'
10
11# Function for querying the JSS API
12# Parameters:
13# (1) URL Suffix for the page that needs to be queried.
14# (2) Value that you are searching for within the JSS API.
15apiQuery() {
16 # Set full URL path to JSS hostname. Example:
17 # jssURL="https://mycompany.mydomain.com:8443"
18 jssURL=""
19
20 # Set JSS API username. Example:
21 # jssUser="CasperAPI"
22 jssUser=""
23
24 # Set JSS API password. Example:
25 # jssPW="password"
26 jssPW=""
27
28 # If requesting JSON response
29 if [ "$1" == "json" ]; then
30 # Get data from JSS and return the JSON response. Add the "-k" switch to the "curl" command if SSL Cert is not trusted.
31 curl "$jssURL/JSSResource/$2" -H 'Accept: application/json' --user "$jssUser:$jssPW" --silent | python -m json.tool
32 # If not requesting the JSON response
33 else
34 # Get data from JSS and return the XML response. Add the "-k" switch to the "curl" command if SSL Cert is not trusted.
35 curl -k "$jssURL/JSSResource/$1" --user "$jssUser:$jssPW" --silent | awk -F "<$2>|</$2>" '{ print $2 }'
36 fi
37}
38
39###########################################################################################################
40############################ OPTION 1 - GET EMAIL ADDRESS FROM COMPUTER RECORD ############################
41###########################################################################################################
42
43# Get hardware UUID/UDID of system
44hwUDID=`system_profiler SPHardwareDataType | awk '/Hardware UUID/{ print $3 }'`
45
46# Get assigned user's email address from the JSS computer record
47assignedUserEmail=`apiQuery "computers/udid/$hwUDID/subset/location" email_address`
48
49###########################################################################################################
50############################## OPTION 2 - GET EMAIL ADDRESS FROM USER RECORD ##############################
51###########################################################################################################
52
53# Get currently logged in user
54targetUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
55");'`
56
57# Get currently logged in user's email address from the JSS user record
58currentUserEmail=`apiQuery "users/name/$targetUser" email`
59
60# Output information
61echo "Assigned User Email Address: $assignedUserEmail"
62echo "Current User Email Address: $currentUserEmail"