Searching the JSS with API calls and Python

Searching the JSS with API calls and Python

r2d2_search

Like a loyal droid, your Jamf Pro server wants to help! (Episode III)


Jamf Software’s Jamf Pro server (JPS) provides an Application Programming Interface, or API, to interact with the JPS database. This allows an enterprise to customize specific areas of the JPS as needed. This post builds on the other articles in the series: Fetching Data From the JSS using API calls & Python and Publishing information to the Jamf Pro server using API calls and Python. We’ll discuss searching for specific data within the JPS.

The inspiration for this series of articles came from 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 small experiment turned into a cross-platform, automated, text- and GUI-based application that talks to our JPS, campus LDAP as well as multiple MySQL databases. The core activity of the application is maintaining the User and Location area of the Inventory pane for each computer in the JPS. It’s called Tugboat.

Here’s an example of Tugboat’s search function is action:

Searching the JPS


Each type of device has a similar GET method for searching inside its space:

/computers/match/{search term}

/mobiledevices/match/{search term}

The following table shows slightly different fields each device categories search encompasses, there are also the fields returned with each match:

Computers


General
asset_tag
bar_code_1
bar_code_2
id
name
Hardware
alt_mac_address
mac_address
serial_number
udid
User and Location
building
building_name
department
department_name
email
email_address
position
realname
room
username

Mobile Devices


General
id
name
Hardware
mac_address
serial_number
udid
wifi_mac_address
User and Location
building
building_name
department
department_name
email
email_address
position
realname
room
username

Breaking down the sample script


Building the search URL

I’ll assemble the search URL in two stages for ease of reading, the base API URL and the full search URL. The jss_base_url  is built using the jss_host string the user enters at the beginning of script. The search_url  is constructed by combining the base API URL with the jss_device_space  (computer or mobiledevice), with the API call ( match ) and the jss_search_term .

The search can be wildcarded by wrapping the search team with asterisks ( * ). This will return any record that includes with search term within any of the supported fields. Note that hitting return in the script on the JSS search string?  prompt will match everything, since it will be an empty wildcard. Removing the asterisks will search only for instances of that specific search term.

Executing the search URL

I pass the search_url to the JPS using the urllib2 module. This module is included in the standard library of Python. Other developers find the Requests module easier to use. If your production environment allows you to install it, I suggest taking a look at it.

Handling the results

Reading the payload of the response involves using the JSON module to organize an easily parse-able data structure.

Displaying the results

The JPS API gets a little odd here, so hang with me for a second. While we may search the mobiledevices  space, the root node of the results is labeled mobile_devices . So we’ll have to use the appropriate label here when we ask for the number of sub-items.

We continue to use the JSON module to pretty print the entire data structure.

Complete sample script


Here is the complete sample script that show’s how to connect to your JPS, request a search and display the results:

Sample output of the running script:

Note

There is an additional computer search /computers/match/name/{matchname} documented in the API. It doesn’t appear to return any results. If anyone has any further info about this call, please let me know in the comments below!

No Comments

Leave a Reply