cancel
Showing results for 
Search instead for 
Did you mean: 
Disclaimer
JUMPCLOUD EXPRESSLY DISCLAIMS ALL REPRESENTATIONS, WARRANTIES, CONDITIONS, AND LIABILITIES OF ANY KIND ARISING FROM OR RELATED TO THIRD-PARTY SOFTWARE, SCRIPTS, REPOSITORIES, AND APIS. JUMPCLOUD IS NOT REQUIRED TO SUPPORT ANY SUCH THIRD-PARTY MATERIALS AND ALL RISKS RELATED TO THIRD-PARTY MATERIALS ARE YOUR RESPONSIBILITY. PLEASE ALSO REVIEW THE JUMPCLOUD TOS.

Mac Bounduser-Serial Set computername + devicename script.

Fulgubbe
Novitiate III

*Script Updated 24/5 Auth issues fixed. 

Hi all!

I could not find a script for renaming mac computers the way i wanted to so wrote this little script (and stole some code tnx!).

It sure can be cleaned up a bit but it works and maybe it can be useful for someone else.

Script fetches JC bound user from managedUsers.json and serial on Macs and renames computer-name to bounduser-serial and host names to serial then updates device names in Jumpcloud to username-serial:

 

 

#!/bin/bash

JCAPIKey='PUT YOUR API KEY HERE'

#Set computer name to Bound user and serial by Jacob.Lantz 2022

bounduser="$(cat /opt/jc/managedUsers.json | sed -n 's/.*"username":"//p' | awk -F '\\",' '{print toupper($1)}')" 

serial="$(system_profiler SPHardwareDataType | grep Serial | sed -n 's/.*: //p')"


# set the hostname, localhostname and computer name

sudo scutil --set ComputerName "$bounduser-$serial"
sudo scutil --set LocalHostName "$serial"
sudo scutil --set HostName "$serial"

echo "computername changed to $bounduser-$serial"

##
## This API call updates the Display Name of the system record.
##

# Parse the systemKey from the conf file.
# The conf file is JSON and can be parsed using JSON.parse() in a supported language.
conf="$(cat /opt/jc/jcagent.conf)"
regex='\"systemKey\":\"([a-zA-Z0-9_]+)\"'

if [[ ${conf} =~ $regex ]] ; then
  systemKey="${BASH_REMATCH[1]}"
fi

# make the api call passing the signature in the authorization header
curl -iq \
  -d "{\"displayName\" : \"$bounduser-$serial\"}" \
  -X "PUT" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H 'x-api-key: '${JCAPIKey}'' \
  --url https://console.jumpcloud.com/api/systems/${systemKey}


exit 0

 

 

14 REPLIES 14

BenGarrison
JumpCloud Alumni
JumpCloud Alumni

Thanks for posting.. This is awesome! 

Shehwar
Novitiate II

Thanks for this amazing script. I do have a question before proceeding with this. We have 2 users bound via JC. One is our admin account for IT and the other is the user. Would this end-up renaming the device to admin or the end user?

bounduser="$(cat /opt/jc/managedUsers.json | sed -n 's/.*"username":"//p' | awk -F '\\",' '{print toupper($1)}')" 

That looks like it's getting the managedUsers.json and then grabbing the username: from it. I would imagine, whatever the first entry was in that JSON would be returned. So likely, this would indeed grab the admin account (I could be way wrong since I haven't tested @Fulgubbe would be a better resource. 

You could change the first line to grab the currently logged in user. But this would require making sure that the username matched a JC user.

boundUser=$USER




Yes that's a valid point I only have one bound user so you probably need to adjust that line a little to exclude admin user.

*edit

sed -n 's/.*"username":"//p'

Accutally removes everything before last username entry in output. So as is it uses the last bound user to computer (tested here).

Shehwar
Novitiate II

 

Hey All,

I've tried this and apparently it renamed it to only serial number 🙃

Not sure why that happened. But I took a look at the path and it says I don't have access.  

/opt/jc/

 Any way to bypass/fix this?

Hi,

Did you run as root?

Yep. Command is running from our Jumpcloud portal as root. Screenshot 2022-04-25 at 11.07.05 AM.png

Shehwar_0-1650881693531.png

 

Hmm tried it myself here and it worked as intended so i don't really know what's going on.

If you run this as root on one of your macs: 

cat /opt/jc/managedUsers.json | sed -n 's/.*"username":"//p' | awk -F '\\",' '{print toupper($1)}'

Do you still get access denied?

So, with some trial and error I got it to work, but it won't change the LocalHostName for some reason. That still stays as the original.

Get this error: when I try to change the criteria to check localHostName instead of ComputerName can the localhostname not contain a "." since it's trying to change it to "Shehwar.Khan-SERIAL"?

SCPreferencesSetLocalHostName() failed: Invalid argument

 

Also, the API call function is giving this error: 

HTTP/1.1 401 Unauthorized
server: nginx
date: Wed, 27 Apr 2022 12:39:49 GMT
content-type: application/json; charset=utf-8
content-length: 49
etag: W/"31-+JMI96BBJfRcU9fDSEXoUOseNyU"
x-response-time: 57.100ms

{"message":"Unauthorized","error":"Unauthorized"} 
SCPreferencesSetLocalHostName() failed: Invalid argument
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100    92  100    49  100    43     50     43  0:00:01 --:--:--  0:00:01    96

 

"can the localhostname not contain a "." since it's trying to change it to "Shehwar.Khan-SERIAL"?"

No it can't that's why local hostname is only serial in the script. The result in a terminal will be username@serial in the end anyway.

 

Hi @Fulgubbe,

Taking some other things into consideration I've reworked this script to my needs. 
However, I'm still getting the unauthorised error and it's failing to update the device name on the JC Admin console.

 

HTTP/1.1 401 Unauthorized
server: nginx
date: Mon, 23 May 2022 12:45:59 GMT
content-type: application/json; charset=utf-8
content-length: 49
etag: W/"31-+JMI96BBJfRcU9fDSEXoUOseNyU"
x-response-time: 15.285ms

{"message":"Unauthorized","error":"Unauthorized"}
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 85 100 49 100 36 55 40 --:--:-- --:--:-- --:--:-- 96

 

 

 

Any hints to why that would fail?

Hmm I seems to get the same error now (on some users). I will try to find time to update the script.

Updated my original post now and cleaned it up a bit. You have to utilize your API key to make it work now. Something seems fishy with the original auth mechanism used by a lot of JC api scripts.

JuergenKlaassen
Rising Star III
Rising Star III

Here's a 'Windows-version' of it now

You Might Like

New to the site? Take a look at these additional resources:

Community created scripts:

Our new Radical Admin blog:

Keep up with Product News:

Read our community guidelines

Ready to join us? You can register here.