EA to report computers default DP

dpertschi
Valued Contributor

I searched the EA's posted here and came up empty. Has anyone written an EA to report the computer's default distribution point? Or is this going to be my crash course introduction to API scripting?

7 REPLIES 7

mm2270
Legendary Contributor III
Or is this going to be my crash course introduction to API scripting?

Yes, it will. The only way I know of to get the assigned DP for a Mac is from the API. You can't get that information locally.

thoule
Valued Contributor II

Here's a basic script. Run this script via a policy on each machine and it'll save the info for you to get later with an EA.

#!/bin/sh
#dumps distribution point and computer ID in a preference file

########### EDIT THESE ##################################
JSSURL="https://jss.edu:8443"
user="apiuser"
pass="xxxxxxxx"
############################################################ 
computerID=`/usr/local/jamf/bin/jamf recon |grep -i "computer_id" | awk -F> '{print $2}'|awk -F< '{print $1}'`
defaults write /Library/Preferences/com.wipro.casper.plist JSS_CompID -int $computerID


curl -H "Accept: application/xml" -sfku "$user:$pass" "$JSS/computers/id/$computerID" -X GET |xmllint --format - > /private/tmp/computer$computerID.xml

distPoint=`xpath /tmp/computer.xml '/computer/general/distribution_point'`
defaults write /Library/Preferences/com.wipro.casper.plist DistPoint -string "$distPoint"

And the EA to read it in.

dp=`defaults read /Library/Preferences/com.wipro.casper.plist DistPoint`
echo "<result>$dp</result>"

dpertschi
Valued Contributor

@thoule Nice. Very helpful, thank you. And the plist domain name is pretty close too! hahaha.

When I run it as is, the plist gets written and the computer ID populated correctly, but then the script fails:

-:1: parser error : Document is empty -:1: parser error : Start tag expected, '<' not found no element found at line 1, column 0, byte 0: at /System/Library/Perl/Extras/5.16/darwin-thread-multi-2level/XML/Parser.pm line 187.

mm2270
Legendary Contributor III

@dpertschi Does the string being pulled from the API for the DP have any odd characters in it? xml is sensitive to certain characters that it uses for tag information. Some characters will confuse it when trying to write or read back the data.

thoule
Valued Contributor II

@dpertschi Take a look at /tmp/computerXX.xml file. About 10 or 20 lines down is the distributionpoint tag. I don't have that in use so I couldn't write complete code for you. As mm says, anything weird in there? And see what's being written to the plist 'defaults read /L/p/com.wick.plist' and see if there's anything in there for DistPoint.

My hope was to give you a script to start with and you can learn that APIfu that you've been meaning to get to ;) Just take it line by line and see where you stop getting the data you want.

stevewood
Honored Contributor II
Honored Contributor II

Or, if you'd like to go with an EA that does the search itself, I cobbled this together for you. Just another reason to practice my Python:

#!/usr/bin/env python

import urllib
import subprocess
import os.path
import xml.etree.ElementTree as ET


jssAPIuser = 'apiuser'
jssAPIpass = 'apipass'
jssURL = 'https://' + jssAPIuser + ':' + jssAPIpass + 
    '@jss.company.com:8443'


def jamf_check():
    if os.path.exists('/usr/sbin/jamf'):
        jamfbin = '/usr/sbin/jamf'
    elif os.path.exists('/usr/local/bin/jamf'):
        jamfbin = '/usr/local/bin/jamf'
    return jamfbin


serial = subprocess.Popen("system_profiler SPHardwareDataType |grep -v tray 
    | awk '/Serial/ {print $4}'", shell=True, stdout=subprocess.PIPE).
    communicate()[0].strip()

url = jssURL + 
    '/JSSResource/computers/serialnumber/' + serial + '/subset/General'
uh = urllib.urlopen(url)
data = uh.read()
tree = ET.fromstring(data)
general = tree.findall('general')
distro_point = general[0].find('distribution_point').text

print '<result>' + str(distro_point) + '</result>'

dpertschi
Valued Contributor

@thoule definitely a good start for me and I'll be picking through it piece by piece to debug here.
@stevewood I'm lucky enough when I can bash through a door, python makes me nervous. But likewise, good example so thanks for that.