API Help - Please

spotter
New Contributor III

I'm working on a script that would be used while a device is in the Netboot state. The script will remove the device from the JSS and Active Directory then format the HD.

The part i'm hoping someone can assist with is how to get the Computer Name based on the UUID. I'm getting the UUID with the following script.

uuid=$(/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice | /usr/bin/grep -i "UUID" | /usr/bin/cut -c27-62)

I'm hoping this is possible so i can avoid having to prompt the tech for the device name.

2 ACCEPTED SOLUTIONS

mm2270
Legendary Contributor III

Yes, its possible. Here's the syntax I would use

/usr/bin/curl -H "Accept: application/xml" -sfku "${apiuser}:${apipass}" https://yourjss.com:8443/JSSResource/computers/udid/${uuid} | xpath /computer/general/name[1] | awk -F'>|<' '/name/{print $3}'

BTW, in the JSS, its actually referred to as the UDID, not UUID, but the info you're grabbing is correct for the curl command. Also, here is a slightly more efficient way to get the UUID/UDID.

/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}'

View solution in original post

GaToRAiD
Contributor II

I agree with @mm2270][/url][/url, but my solution would work a little different.

#!/bin/sh
######################################################################## 
# User changable settings...
######################################################################## 

jssUser='username'
jssPass='password'
jssHost=https://jssurl.com:8443/

######################################################################## 
# Code below
######################################################################## 

OSVersion=$(sw_vers -productVersion)
uuid=$( /usr/sbin/system_profiler -xml SPHardwareDataType | /usr/bin/xpath "//array/dict/array/dict/string[preceding-sibling::key='platform_UUID'][1]/text()" 2> /dev/null )

xmlComputerInfo="<?xml version="1.0" encoding="ISO-8859-1" ?>
<computer>
</computer>"

theJSSresponse=$( /usr/bin/curl 
    --header "Content-Type: text/xml; charset=utf-8" 
    --data "${xmlComputerInfo}" 
    --request GET 
    --connect-timeout 5 
    --max-time 10 
    --user ${jssUser}:${jssPass} 
    ${jssHost}JSSResource/computers/udid/${uuid} 2> /dev/null )

computerName=`echo $theJSSResponse | xpath /computer/general/name[1] | awk -F'>|<' '/name/{print $3}'`

*Note* I have not tested to code. You might need to change some things to make it work for you.

Hope either one of these helps.

View solution in original post

5 REPLIES 5

mm2270
Legendary Contributor III

Yes, its possible. Here's the syntax I would use

/usr/bin/curl -H "Accept: application/xml" -sfku "${apiuser}:${apipass}" https://yourjss.com:8443/JSSResource/computers/udid/${uuid} | xpath /computer/general/name[1] | awk -F'>|<' '/name/{print $3}'

BTW, in the JSS, its actually referred to as the UDID, not UUID, but the info you're grabbing is correct for the curl command. Also, here is a slightly more efficient way to get the UUID/UDID.

/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}'

GaToRAiD
Contributor II

I agree with @mm2270][/url][/url, but my solution would work a little different.

#!/bin/sh
######################################################################## 
# User changable settings...
######################################################################## 

jssUser='username'
jssPass='password'
jssHost=https://jssurl.com:8443/

######################################################################## 
# Code below
######################################################################## 

OSVersion=$(sw_vers -productVersion)
uuid=$( /usr/sbin/system_profiler -xml SPHardwareDataType | /usr/bin/xpath "//array/dict/array/dict/string[preceding-sibling::key='platform_UUID'][1]/text()" 2> /dev/null )

xmlComputerInfo="<?xml version="1.0" encoding="ISO-8859-1" ?>
<computer>
</computer>"

theJSSresponse=$( /usr/bin/curl 
    --header "Content-Type: text/xml; charset=utf-8" 
    --data "${xmlComputerInfo}" 
    --request GET 
    --connect-timeout 5 
    --max-time 10 
    --user ${jssUser}:${jssPass} 
    ${jssHost}JSSResource/computers/udid/${uuid} 2> /dev/null )

computerName=`echo $theJSSResponse | xpath /computer/general/name[1] | awk -F'>|<' '/name/{print $3}'`

*Note* I have not tested to code. You might need to change some things to make it work for you.

Hope either one of these helps.

spotter
New Contributor III

I really appreciate the reply's however when running either code i get the following error...

no element found at line 2, column 0, byte 1: ^ at /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level/XML/Parser.pm line 187.

GaToRAiD
Contributor II

@Potter][/url][/url that is xpath not parsing out the xml properly. what you need to do is just echo out the "theJSSresponse" and see what you need to work with path.

Here is a bit dirtier of markup of the xpath parse.

echo $theJSSresponse | xpath /computer/general/name[1] | sed 's/<name>//g' | sed 's/</name>//g'

I did test the whole script this time, and I can say it does work.

spotter
New Contributor III

so after troubleshooting i finally got the script to produce the information i was needing...

a huge THANK YOU to @GaToRAiD and @mm2270 for your scripts

I owe you both a beer or drink at JNUC 2015....