-
Notifications
You must be signed in to change notification settings - Fork 1
/
host_name_changer2.5.1j.sh
103 lines (91 loc) · 3.05 KB
/
host_name_changer2.5.1j.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
#welcome to zackn9nes jamf hostname script
#do smartgroups to parse location since we don't want API calls in scripts for SEC reasons
#if not using with jamf set LOCATION here
#if using JAMF set LOCATION in $4
LOCATION="NY"
#jamfmode logic
JAMFBINARY=$(/usr/bin/which jamf)
if test -f "$JAMFBINARY"; then
JAMFMODE=true
echo "$JAMFBINARY exists looking for location var in $4"
LOCATION="$4"
fi
#curl apples machine db against last 4 of serial
#get last four serial for year
lastFourSerialForAPPL=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | grep -o '....$')
echo "last four is" $lastFourSerialForAPPL
#use that (serial) for actual year
MNF_YEAR=$(curl "https://support-sp.apple.com/sp/product?cc=$(echo $lastFourSerialForAPPL)" | grep -Eo '[0-9]{4}')
echo "determined year is" $MNF_YEAR
#get machines info locally
initialhweval=$(sysctl hw.model)
if [[ $initialhweval == *"Pro"* ]]; then
echo "Model: MBP"
MODEL="MBP"
elif [[ $initialhweval == *"Air"* ]]; then
echo "Model: MBA"
MODEL="MBA"
elif [[ $initialhweval == *"MacBook8"* ]]; then
echo "Model: MB12"
MODEL="MB"
elif [[ $initialhweval == *"MacBook9"* ]]; then
echo "Model: MB12"
MODEL="MB"
elif [[ $initialhweval == *"MacBook10"* ]]; then
echo "Model: MB12"
MODEL="MB"
elif [[ $initialhweval == *"iMac"* ]]; then
echo "Model: iMac"
MODEL="iMac"
elif [[ $initialhweval == *"mini"* ]]; then
echo "Model: mini"
MODEL="Mini"
elif [ -z "$initialhweval" ]; then
echo "computer unknown"
exit 1
echo "Found a ${MODEL}"
fi
#check user locally
USER=$(scutil <<<"show State:/Users/ConsoleUser" | awk -F': ' '/[[:space:]]+Name[[:space:]]:/ { if ( $2 != "loginwindow" ) { print $2 }}')
echo "detected user is:" $USER
#sanitize user
USER=$(echo $USER | sed 's/[^a-zA-Z0-9]//g')
#killswitch conditions
if [ -z "$USER" ]; then
echo "Error: user is null exiting with error"
exit 1
elif [ $USER = "root" ]; then
echo "Error: user is root user failing gracefully"
exit 1
elif (($MNF_YEAR <= 2000)); then
echo "year is too low"
#get a random number generator ready once per script run should be fine
random=$(echo "$((1000 + RANDOM % 9000))")
$MNF_YEAR=$random
elif (($MNF_YEAR >= 2040)); then
echo "year is too high"
#get a random number generator ready once per script run should be fine
random=$(echo "$((1000 + RANDOM % 9000))")
$MNF_YEAR=$random
elif [ -z "$MODEL" ]; then
echo "Error: Model is broken exiting with error"
exit 1
fi
#payload
HOSTNAME="${LOCATION}-${MODEL}-${MNF_YEAR}-${USER}"
#do all the things logic
if [ "$JAMFMODE" == true ]; then
#do all of the things with $JAMFBINARY
echo "hostname will be: ${HOSTNAME}"
$JAMFBINARY setComputerName -name "$HOSTNAME"
$JAMFBINARY recon
elif [ "$JAMFMODE" != true ]; then
#do all of the things in a general format
echo "hostname will be: ${HOSTNAME}"
sudo scutil --set HostName "$HOSTNAME"
sudo scutil --set ComputerName "$HOSTNAME"
sudo scutil --set LocalHostName "$HOSTNAME"
dscacheutil -flushcache
fi
exit 0