-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_macOS_hostname.py
More file actions
81 lines (61 loc) · 2.45 KB
/
set_macOS_hostname.py
File metadata and controls
81 lines (61 loc) · 2.45 KB
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
'''
1. Open a terminal.
2. Type the following command to change the primary hostname of your Mac:
This is your fully qualified hostname, for example myMac.domain.com
sudo scutil --set HostName <new host name>
So for example:
sudo scutil --set HostName flame01.domain.com
3. Type the following command to change the Bonjour hostname of your Mac:
This is the name usable on the local network, for example myMac.local.
sudo scutil --set LocalHostName <new host name>
So for example:
sudo scutil --set LocalHostName flame01
4. Type the following command to change the computer name:
This is the user-friendly computer name you see in Finder, for example myMac.
sudo scutil --set ComputerName <new name>
So for example:
sudo scutil --set ComputerName flame01
5. Flush the DNS cache by typing: dscacheutil -flushcache
6. Restart Mac.
'''
import os, sys, time
yes = ["y", "Y"]
cmdPrefix = 'sudo scutil'
setFlag = '--set'
getFlag = '--get'
flushCache = 'dscacheutil -flushcache
'
fqdn = sys.argv[1]
host, domain = fqdn.split('.', 1)
dict = {
'primary': {'label': 'Primary Hostname', 'command': 'HostName', 'string': fqdn},
'bonjour': {'label': 'Bonjour Hostname', 'command': 'LocalHostName', 'string': host},
'computer': {'label': 'Computer Name', 'command': 'ComputerName', 'string': host}
}
print("You are about to change your hostname to: " + fqdn)
print("This will require a system restart to fully take effect.")
print("Proceed? y / n")
confirmation = input()
if confirmation not in yes:
print("Exiting......")
sys.exit()
else:
print("Commencing system configuration; your password may be required.")
for key in dict.keys():
# Set
print("Changing \'{}\' to \'{}\'".format(dict[key]['label'], dict[key]['string']))
setCommand = os.system("{} {} {} {}".format(cmdPrefix, setFlag, dict[key]['command'], dict[key]['string']))
# Verify
if setCommand == 0:
print("Your {} is now \'{}\'\n".format(dict[key]['label'], dict[key]['string']))
else:
print("Failed to change {}. Exiting.".format(dict[key]['label']))
sys.exit()
# Flush DNS cache
flushCommand = os.system("{}".format(flushCache))
print("Successfully set machine hostname. Do you want to restart now? y / n")
reboot = input()
if reboot not in yes:
print("Changes will take effect upon next system restart.")
sys.exit()
else:
print("Performing system restart in 5 seconds.")
print("Press CTRL + c to terminate system restart")
time.sleep(5)
restart = os.system("sudo shutdown -r now")