04-01-2022 08:23 AM - edited 05-24-2022 10:37 AM
*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
04-01-2022 08:37 AM
Thanks for posting.. This is awesome!
04-05-2022 05:59 AM
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?
04-05-2022 09:11 AM
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
04-05-2022 09:15 AM - edited 04-05-2022 09:39 AM
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).
04-25-2022 05:45 AM
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?
04-25-2022 06:05 AM
Hi,
Did you run as root?
04-25-2022 06:09 AM - edited 04-25-2022 06:15 AM
Yep. Command is running from our Jumpcloud portal as root.
04-25-2022 06:26 AM - edited 04-25-2022 06:26 AM
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?
04-27-2022 09:27 AM
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
04-27-2022 10:53 AM
"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.
05-23-2022 08:54 AM - edited 05-23-2022 08:55 AM
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?
05-24-2022 04:39 AM
Hmm I seems to get the same error now (on some users). I will try to find time to update the script.
05-24-2022 10:35 AM
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.
02-17-2023 06:12 AM
Here's a 'Windows-version' of it now
New to the site? Take a look at these additional resources:
Ready to join us? You can register here.