Skip to content

Instantly share code, notes, and snippets.

@pudquick
Created January 4, 2017 16:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pudquick/83ec0ce0a5f3dbc52e70c8c1ce64a389 to your computer and use it in GitHub Desktop.
Save pudquick/83ec0ce0a5f3dbc52e70c8c1ce64a389 to your computer and use it in GitHub Desktop.
Extract serial number out of .spx files generated by System Profiler on macOS
#!/usr/bin/python
import plistlib, sys
# Grab the path to the spx file
file_path = sys.argv[1]
# ingest the spx (which is a plist)
file_data = plistlib.readPlist(file_path)
# find the item within it that has the following values:
# _parentDataType: SPRootDataType
# _dataType: SPHardwareDataType
candidates = []
for x in file_data:
if x.get('_parentDataType', None) == 'SPRootDataType':
if x.get('_dataType', None) == 'SPHardwareDataType':
candidates.append(x)
# Only emit a serial if we have one
for x in candidates:
try:
print x['_items'][0]['serial_number']
except:
pass
@williamlombard
Copy link

williamlombard commented Sep 24, 2017

That is a super useful script.

Can you add a few more lines so that one can return information such as hard drive size etc?

I've tried this but am getting no success...

candidates2 =[]
for y in file_data:
if y.get('_parentDataType', None) == 'SPRootDataType':
if x.get('_dataType', None) == 'SPSerialATADataType':()
candidates2.append(y)

for y in candidates2:
print y['_items'][0]['size_in_bytes']

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment