forked from edingc/munki-enroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.py
executable file
·115 lines (100 loc) · 4.51 KB
/
inventory.py
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
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
import csv
import subprocess
import plistlib
import sys
import urllib2
CSV_LOCATION = 'http://your.domain/imaging/reporting/inventory.csv'
CSV = urllib2.urlopen(CSV_LOCATION)
def get_hardware_info():
'''Uses system profiler to get hardware info for this machine'''
cmd = ['/usr/sbin/system_profiler', 'SPHardwareDataType', '-xml']
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, unused_error) = proc.communicate()
try:
plist = plistlib.readPlistFromString(output)
# system_profiler xml is an array
sp_dict = plist[0]
items = sp_dict['_items']
sp_hardware_dict = items[0]
return sp_hardware_dict
except Exception:
return {}
def check_compname(serial_number):
csv_data = csv.DictReader(CSV, delimiter=',')
for row in csv_data:
serial = row['serial']
computername = row['name']
hostname = row['name'].replace(' ','')
catalog_tag = row['catalog']
area_tag = row['area']
room_tag = row['room']
asset_tag = row['asset']
if serial == serial_number:
set_sharingname(computername)
set_hostname(hostname)
set_localhostname(hostname)
set_catalog_tag(catalog_tag)
set_area_tag(area_tag)
set_room_tag(room_tag)
set_asset_tag(asset_tag)
def get_serial_number():
hardware_info = get_hardware_info()
return hardware_info.get('serial_number', 'UNKNOWN')
def set_sharingname(computername):
cmd = ['sudo', '/usr/sbin/scutil', '--set', 'ComputerName', computername]
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, unused_error) = proc.communicate()
return output.rstrip('\n')
def set_hostname(hostname):
cmd = ['sudo', '/usr/sbin/scutil', '--set', 'HostName', hostname]
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, unused_error) = proc.communicate()
return output.rstrip('\n')
def set_localhostname(hostname):
cmd = ['sudo', '/usr/sbin/scutil', '--set', 'LocalHostName', hostname]
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, unused_error) = proc.communicate()
return output.rstrip('\n')
def set_catalog_tag(catalog_tag):
cmd = ['sudo', '/usr/bin/defaults', 'write', '/Library/Preferences/com.apple.RemoteDesktop.plist', 'Text1', '-string', catalog_tag]
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, unused_error) = proc.communicate()
return output.rstrip('\n')
def set_area_tag(area_tag):
cmd = ['sudo', '/usr/bin/defaults', 'write', '/Library/Preferences/com.apple.RemoteDesktop.plist', 'Text2', '-string', area_tag]
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, unused_error) = proc.communicate()
return output.rstrip('\n')
def set_room_tag(room_tag):
cmd = ['sudo', '/usr/bin/defaults', 'write', '/Library/Preferences/com.apple.RemoteDesktop.plist', 'Text3', '-string', room_tag]
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, unused_error) = proc.communicate()
return output.rstrip('\n')
def set_asset_tag(asset_tag):
cmd = ['sudo', '/usr/bin/defaults', 'write', '/Library/Preferences/com.apple.RemoteDesktop.plist', 'Text4', '-string', asset_tag]
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, unused_error) = proc.communicate()
return output.rstrip('\n')
def main():
serial_number = get_serial_number()
check_compname(serial_number)
if __name__ == '__main__':
main()
sys.exit(0)