Last active
May 29, 2019 20:11
-
-
Save PinkShellos/6914b4251cd15da402cf4f680f5f951a to your computer and use it in GitHub Desktop.
Generate Mac name based on username and model id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# This script is to automate the naming process that our Macs follow: FirstnameLastname-Model | |
import subprocess | |
import sys | |
import os | |
from SystemConfiguration import SCDynamicStoreCopyConsoleUser | |
# dictionary matching our model naming schemes for Macs to their model identifiers | |
model_id_dict = {'15MBP': ['MacBookPro15,1', 'MacBookPro14,3', 'MacBookPro13,3', | |
'MacBookPro11,4', 'MacBookPro11,5', 'MacBookPro11,2', | |
'MacBookPro11,3', 'MacBookPro10,1', 'MacBookPro9,1'], | |
'13MBP': ['MacBookPro15,2', 'MacBookPro14,2', 'MacBookPro14,1', | |
'MacBookPro13,2', 'MacBookPro13,1', 'MacBookPro12,1', | |
'MacBookPro11,1', 'MacBookPro10,2', 'MacBookPro9,2'], | |
'27iMac': ['iMac18,3', 'iMac15,1', 'iMac17,1', 'iMac15,1', | |
'iMac14,2', 'iMac13,2', 'iMac12,2', 'iMac11,3'], | |
'21iMac': ['iMac18,1', 'iMac18,2', 'iMac16,1', 'iMac16,2', | |
'iMac14,4', 'iMac14,1', 'iMac13,1', 'iMac12,1', | |
'iMac11,2']} | |
# variable for to store results of model id bash command | |
fetch_model_id = subprocess.check_output(['sysctl','hw.model']) | |
search_model_id = fetch_model_id.strip('hw.model:').strip('\n').strip() | |
model_name = '' | |
# reverse dictionary lookup to set model name based on model id | |
for name, model_id in model_id_dict.items(): | |
if search_model_id in model_id: | |
model_name = name | |
break | |
else: | |
model_name = 'Mini' | |
def get_console_user(): | |
'''Uses Apple's SystemConfiguration framework to get the current | |
console user''' | |
cfuser = SCDynamicStoreCopyConsoleUser(None, None, None) | |
return cfuser[0] | |
console_user = get_console_user() | |
user_real_name = subprocess.check_output(['id','-F', console_user]) | |
computer_name = ''.join(user_real_name.split()) + '-' + model_name | |
def set_computer_name(cname): | |
'''function to set hostname, localhost, and computer name | |
to the same value and flush the cache''' | |
commands = ['HostName', 'LocalHostName', 'ComputerName'] | |
for name_type in commands: | |
subprocess.call(['scutil', '--set', name_type, cname]) | |
subprocess.call(['jamf', 'setComputerName', '-name', cname]) | |
subprocess.call(['dscacheutil', '-flushcache']) | |
if user_real_name == 'Local Administrator': | |
sys.exit(0) | |
else: | |
set_computer_name(computer_name) | |
check_computer_name = subprocess.check_output(['scutil', '--get', 'ComputerName']) | |
if check_computer_name == computer_name: | |
sys.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Disable macOS Upgrade notifications | |
# Step 1: prevent the update which brings the notification down | |
softwareupdate --ignore macOSInstallerNotification_GM | |
echo | |
# Step 2: delete the file if it's already on the computer | |
if [[ -d /Library/Bundles/OSXNotification.bundle ]]; then | |
echo "OSXNotification.bundle found. Deleting..." | |
rm -rf /Library/Bundles/OSXNotification.bundle ||: | |
else | |
echo "OSXNotification.bundle not found." | |
fi |
If I ssh into another machine as root, and run id -P
I get an entry for root (which is what I expected). Since Jamf runs this script as root, wouldn't it always get info for root?
Different point: if all you want is a username, id -un
gives you the same without the need to split the output.
@gregneagle we use the full name of the user with camel case naming and user name seems to be set willy-nilly across devices without standard. if there's a way to query for the full name of the logged in user without needing to clean up the string, that'd be cool.
Oh id -F
🤦♂️
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is to generate a Mac's user name based on their computer's model identifier and their user name. Our naming structure follows the process of
FirstnameLastname-Model
(e.g.:MontyPython-15MBP
). This script is meant to be executed by Jamf, which will always run it as root.Comments and code review welcome!