Jamf API xmllint --xpath Question

druocco
New Contributor III

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?

1 ACCEPTED SOLUTION

druocco
New Contributor III

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 solution in original post

2 REPLIES 2

boberito
Valued Contributor

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

 

druocco
New Contributor III

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 solution in original post