A Jamf Pro API Helper in Bash

Most simple Jamf API scripts you see getting shared around are written in Bash. They don’t have to do anything super complicated, Bash is built into macOS, and it’s easier to learn than most other common scripting languages.

That was fine for a long time. A quick curl one-liner could get you all the data you needed. But Jamf is starting to make some changes to do a better job of security, handling way larger device fleets, and making new kinds of useful data available. So things are getting trickier. Auth is changing and the json offered up by the new API isn’t easy to deal with unless you install some other tools. So it’s probably time to switch to a new language if you’re up for it. The learning curve is a steep compared to basic Bash, but it pays off eventually.

But, if you’re not ready to go there, here are some complications/techniques you might come across…

  • Named function parameters
  • Variable scope in bash
  • Calling functions and assigning output to a variable
  • Returning multiple values from functions (albeit in global vars)
  • Options to log to file or stdout
  • Use of curl timeouts
  • How to handle session cookies
  • Returning http status for API calls
  • Getting API credentials from keychain
  • Handles Jamf Pro API auth for you, i.e., fetches auth tokens as needed
  • Refreshing auth tokens as they near expiration/Clearing them when done.
  • Error messages relevant to the Jamf Pro API
  • Demonstrate how to parse out data elements
  • Convert child object data (e.g. lists of computer names) to iterable arrays
  • Extract data from json

There’s an example script in https://github.com/macnotes/jamfscripts/tree/main/jamfpro/jamfProApiHelper. If you find something interesting there, you can pull it out and use it in your own scripts.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: