Skip to main content
Solved

Jamf API xmllint --xpath Question

  • Aug 3, 2021 3:30 PM UTC
  • 3 replies
  • 90 views

Forum|alt.badge.img+9

Searched Jamf Nation but couldn't find anything close enough to my use case. 

I'm querying the Jamf API for computer IDs by way of usernames:

jamfUserURL="https://[company].jamfcloud.com/JSSResource/users/name/$username" computerID="$(curl -X GET -s -k -u "$jamfUser":"$jamfPass" "$jamfUserURL" | xmllint --xpath '/user/links/computers/computer/id/text()' - )" echo $computerID

 This works great except for end users who have more than one computer in the environment. Then I get a result with all IDs combined into one return. Would it make sense to stick this in a for loop to iterate through the multiple IDs? If so, can someone please assist?

Best answer by druocco

Thanks @boberito! I'll give this a run and see what happens.

I actually stumbled on another way as well:

computerID="$(curl -X GET -s -k -u "$jamfUser":"$jamfPass" "$jamfUserURL" | xmllint --xpath '/user/links/computers/computer/id' - | awk -F'>|<' '/id/{print $3}')"

The 'awk' did it. 

View original

3 replies

boberito
Forum|alt.badge.img+22
  • Jamf Heroes
  • Aug 3, 2021 6:36 PM UTC

You have to do something funky 

Due to the version of xmllint included with macOS it doesn't have the argument to split things off to different lines easily. I ran into this recently and came up with this solution. You can just change some pieces to meet your need

 

curl -s -u "${jamfaccount}:${jamfpass}" -X GET "${jamfurl}JSSResource/advancedcomputersearches/id/${acsID}" -H 'Accept: text/xml' > curloutput computers="$(xmllint --shell <<< "cat /advanced_computer_search/computers/computer/id/text()" curloutput)" computerIDs="$(echo $computers | awk -F" ------- " '{$1=""; print $0}')" for computerID in $computerIDs; do re='^[0-9]+$' if ! [[ $computerID =~ $re ]] ; then continue fi echo $computerID done

 


Forum|alt.badge.img+9
  • Author
  • Contributor
  • Answer
  • Aug 3, 2021 6:47 PM UTC

Thanks @boberito! I'll give this a run and see what happens.

I actually stumbled on another way as well:

computerID="$(curl -X GET -s -k -u "$jamfUser":"$jamfPass" "$jamfUserURL" | xmllint --xpath '/user/links/computers/computer/id' - | awk -F'>|<' '/id/{print $3}')"

The 'awk' did it. 


Forum|alt.badge.img+5
  • Contributor
  • Mar 17, 2023 12:03 AM UTC

I get 

 

-:1: parser error : Start tag expected, '<' not found

?PNG

^