JAMF api for fetching building details not working.

vysakjayadas
New Contributor

Hello All,

We were using following script to rename the computer name and it was working before 2 months.

#!/bin/bash

# jamf Api
apiUser="####"
apiPass="####"
jssURL="our_jamf_instance_url"

serial=$(system_profiler SPHardwareDataType | awk '/Serial\ Number\ \(system\)/ {print $NF}')
serialc=$(system_profiler SPHardwareDataType | awk '/Serial\ Number\ \(system\)/ {print substr($0,length-5)}')

base=$(curl -H "Accept: text/xml" -sfku "${apiUser}:${apiPass}" "${jpsURL}/JSSResource/computers/serialnumber/${serial}/subset/Location" | xmllint --xpath "/computer/location/building/text()" -)

echo Building:$base

scutil --set ComputerName $base-$serialc-M
scutil --set HostName $base-$serialc-M
scutil --set LocalHostName $base-$serialc-M

cname=$(scutil --get ComputerName)

echo ComputerName:$cname

exit 0

 

Now the script is returning error 

cript result: -:1: parser error : Document is empty

^
Building:
scutil: invalid option -- 8
usage: scutil
interactive access to the dynamic store.

or: scutil --prefs [preference-file]
interactive access to the [raw] stored preferences.

or: scutil [-W] -r nodename
or: scutil [-W] -r address
or: scutil [-W] -r local-address remote-address
check reachability of node, address, or address pair (-W to "watch").

or: scutil -w dynamic-store-key [ -t timeout ]
-w wait for presense of dynamic store key
-t time to wait for key

or: scutil --get pref
or: scutil --set pref [newval]
or: scutil --get filename path key
pref display (or set) the specified preference. Valid preferences
include:
ComputerName, LocalHostName, HostName
newval New preference value to be set. If not specified,
the new value will be read from standard input.

or: scutil --dns
show DNS configuration.

or: scutil --proxy
show "proxy" configuration.

or: scutil --nwi
show network information

or: scutil --nc
show VPN network configuration information. Use --nc help for full command list

or: scutil --allow-new-interfaces [off|on]
manage new interface creation with screen locked.

or: scutil --error err#
display a descriptive message for the given error code
scutil: invalid option -- 8
usage: scutil
interactive access to the dynamic store.

or: scutil --prefs [preference-file]
interactive access to the [raw] stored preferences.

or: scutil [-W] -r nodename
or: scutil [-W] -r address
or: scutil [-W] -r local-address remote-address
check reachability of node, address, or address pair (-W to "watch").

or: scutil -w dynamic-store-key [ -t timeout ]
-w wait for presense of dynamic store key
-t time to wait for key

or: scutil --get pref
or: scutil --set pref [newval]
or: scutil --get filename path key
pref display (or set) the specified preference. Valid preferences
include:
ComputerName, LocalHostName, HostName
newval New preference value to be set. If not specified,
the new value will be read from standard input.

or: scutil --dns
show DNS configuration.

or: scutil --proxy
show "proxy" configuration.

or: scutil --nwi
show network information

or: scutil --nc
show VPN network configuration information. Use --nc help for full command list

or: scutil --allow-new-interfaces [off|on]
manage new interface creation with screen locked.

or: scutil --error err#
display a descriptive message for the given error code
scutil: invalid option -- 8
usage: scutil
interactive access to the dynamic store.

or: scutil --prefs [preference-file]
interactive access to the [raw] stored preferences.

or: scutil [-W] -r nodename
or: scutil [-W] -r address
or: scutil [-W] -r local-address remote-address
check reachability of node, address, or address pair (-W to "watch").

or: scutil -w dynamic-store-key [ -t timeout ]
-w wait for presense of dynamic store key
-t time to wait for key

or: scutil --get pref
or: scutil --set pref [newval]
or: scutil --get filename path key
pref display (or set) the specified preference. Valid preferences
include:
ComputerName, LocalHostName, HostName
newval New preference value to be set. If not specified,
the new value will be read from standard input.

or: scutil --dns
show DNS configuration.

or: scutil --proxy
show "proxy" configuration.

or: scutil --nwi
show network information

or: scutil --nc
show VPN network configuration information. Use --nc help for full command list

or: scutil --allow-new-interfaces [off|on]
manage new interface creation with screen locked.

or: scutil --error err#
display a descriptive message for the given error code
ComputerName:mymac

 

Do you have any idea how to fix this?Please suggest.

3 REPLIES 3

jamf-42
Valued Contributor II

its 'a bad idea' to call the API from a script.. JAMF API was never designed for this and it not secure, despite what people think. 

 

Use a config profile with variable, then read that back in the script

https://learn.jamf.com/en-US/bundle/jamf-pro-documentation-current/page/Computer_Configuration_Profi...

 

Couldn’t agree more. Using the API client-side is inherently risky, even with the best-case scenario in mind.

When you design your workflow this way, you're inadvertently exposing read access to each of your computer records in Jamf, to anyone who has standard user access to any of the enrolled clients that this script is run on.  

You should re-think this workflow with @jamf-42's idea in mind, or re-architect it in a way that lets you perform these actions in a server <-> server manner... not;  client <-> server 

AlexHoffman
New Contributor III

If you're using basic authentication that is likely your issue. Jamf fully deprecated basic authentication for both the classic and new APIs. You have to use a bearer token authentication for all API calls now.