Skip to main content
Solved

Jamf API xmllint --xpath Question


Forum|alt.badge.img+8

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
Did this topic help you find an answer to your question?

3 replies

boberito
Forum|alt.badge.img+22
  • Jamf Heroes
  • 449 replies
  • August 3, 2021

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+8
  • Author
  • Contributor
  • 14 replies
  • Answer
  • August 3, 2021

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
  • 32 replies
  • March 17, 2023

I get 

 

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

?PNG

^


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings