Reading Casper Extension Attributes with a shell script via the API

I recently had a need to get the value of Casper EAs in a script.
Bryson Tyrrell has two great articles about working with the API here and here which helped me getting started.

Let’s look at what the Extension Attributes look like on our JSS:

https://your.jss.org:8443/JSSResource/computers/macaddress/aa.bb.cc.dd.ee.ff/subset/extensionattributes

(Replace your.jss.org with your JSS URL and aa.bb.cc.dd.ee.ff with the MAC address of a managed client).

Depending on which EAs you have set up, it will look something like this:

JSS Extension AttributesNote that all child-nodes of “extension_attributes” are called “attribute“.
Therefor we have to look for the “name” of the EA we want and get the corresponding “value“.

Bryson already described how to use the Xpath command-line tool,
so we just have to tell Xpath to look for a certainĀ “name” in the child nodes of all occurrences of “attribute“.

Here’s the script i came up with:

#!/bin/sh

apiURL="https://your.jss.org:8443/JSSResource/computers/macaddress/"  #replace your.jss.org with your JSS URL
apiUser="casperapiuser"  #JSS-user with API read privileges
apiPass="casperapipass"  #password of the JSS-user
MacAdd=$(networksetup -getmacaddress en0 | awk '{ print $3 }' | sed 's/:/./g')
extAttName="\"EA Name\"" #name of the EA you're looking for

value=$(curl -s -u $apiUser:$apiPass $apiURL$MacAdd/subset/extension_attributes | xpath "//attribute[name=$extAttName]" 2>&1 | awk -F'<value>|</value>' '{print $2}')


echo $value