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:

(Replace with your JSS URL and 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:


apiURL=""  #replace 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

One thought on “Reading Casper Extension Attributes with a shell script via the API

  1. This definitely go me on the right path, thanks!

    The one catch is string matches vs numbers of EA’s XML node based on value. Nodes with numbers can be like [id=13] but _string_ matches must be single quoted [name=’EAName’]

    Thanks again!

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.