Fetching Data From the JSS using API calls & Python

Fetching Data From the JSS using API calls & Python

bb8_thumbsup

Like a loyal droid, your JSS wants to help!


JAMF Software’s Casper Suite provides an Application Programming Interface, or API, to communicate with the JAMF Software Server (JSS) database. This allows an enterprise to customize specific areas of the JSS as needed. This is the first in a series of articles that will discuss these APIs and walk through building Python scripts that take advantage of the data available in the JSS. Future articles will show how to upload data to the JSS, building Extension Attributes in the JSS to react to that data and more.

These articles are based on an internal inventory application that began as an experiment I described in a previous post: Using GIF images in Python GUI scripts. Since that post was written the experiment turned into a cross-platform, automated, text- and GUI-based application that talks to the JSS, campus LDAP as well as two MySQL databases. The core of the application is maintaining the User and Location area of the Inventory pane for each computer in the JSS.

Requesting information from the JSS


There are a number of ways to get computer info from the JSS. The computer’s hostname or JSS ID will work. I elected to use the UUID, or Universally Unique IDentifier because it’s a cross-platform solution. Here are examples for obtaining UUIDs on macOS and Windows:

Getting the Macintosh UUID

Getting the Windows UUID

 Making the connection


Once you’ve selected a method of identifying the machine, you will then need to request the data from the JSS. This is done by submitting a specific URL to the JSS. Python has a number of modules available to perform this action. I’ve included examples using the included urllib2 and the very popular Requests module. If you choose the requests method, you will need to make sure the module is installed on any machine you attempt to use your script on.

The code breaks down to the following steps:

  1. Assembling the URL
  2. Submitting the URL
  3. Storing the reply
  4. Creating a JSON object to contain the organized contents of the reply
Assembling the URL

urllib2:

requests:

 Handling the data


The JSS will reply to your request in a JSON-formatted message. The JavaScript Object Notation is a common, easy to manipulate, human readable data format. The message will be arranged in something like a tree, requiring you to navigate various branches (keys) to find the leaf (data) you require. Fortunately, the json module makes it very easy to find the information you’re looking for.

Once we’ve stored the reply in a json object, we can use the following command to print the entire object. The quantity of data isn’t something that would be useful to an end user, but you as the developer, will need to see it to familiarize yourself with the structure. A computer record from our JSS is nearly 5,000 lines.

The following code snippet shows direct navigation and processing the extension attributes of a machine to find a specific value. Each extension attribute contains the JSS ID for the EA and the name of the EA, this example utilizes the name. Again, refer to the entire JSON object to build the specific path to the information you need.

General User and Location data

Parsing the Extended Attributes for specific information

More Data from the JSS


The JSS offers much, much more information. For example we can ask the JSS to give us all of the buildings or departments we’ve defined, this information could be used to build a menu offering the user a valid set of choices. This data is located in a different location so we’ll need to change to URL we will call to reflect it.

urllib2:

Requests:

 Massaging the data


The JSON object containing the department information needs to be cleaned up as we’re only interested in the names of the departments.

This section examines each branch and stores the name  in the departments  list.

Your API User


You should seriously consider creating a dedicated “API User” with the minimum required privileges. Should this account be compromised, the damage would be minimized. In System Settings:JSS User Accounts & Groups. Creating a new user with a Privilege Set of Auditor is easiest. Then uncheck every box, in the JSS Settings and in every panel down to Casper Imaging. This gives your api user a limited amount of Read privileges.

Screen Shot 2016-06-28 at 10.25.43 AM

 Putting it all together


Here is the complete sample script, if you have the requests module available you can comment out or remove the urlllib2 sections. You will need to supply the correct URL to your JSS and the credentials for your “API User”.

 Output of the script


Here’s the output of the sample script running on our JSS. If you haven’t defined any departments, your results will be different.

Acknowledgements


Without the following resources, my project wouldn’t have progressed as quickly as I did.

http://macbrained.org/the-jss-rest-api-for-everyone/
https://unofficial-jss-api-docs.atlassian.net/wiki

No Comments

Leave a Reply