Skip to content

Instantly share code, notes, and snippets.

@erikng
Forked from pudquick/get_platform.py
Created February 9, 2016 16:37
Show Gist options
  • Save erikng/46646ff81e55b42e5cfc to your computer and use it in GitHub Desktop.
Save erikng/46646ff81e55b42e5cfc to your computer and use it in GitHub Desktop.
Get Mac's serial number and hardware UUID via python
import objc
from Foundation import NSBundle
IOKit_bundle = NSBundle.bundleWithIdentifier_('com.apple.framework.IOKit')
functions = [("IOServiceGetMatchingService", b"II@"),
("IOServiceMatching", b"@*"),
("IORegistryEntryCreateCFProperty", b"@I@@I"),
]
objc.loadBundleFunctions(IOKit_bundle, globals(), functions)
def io_key(keyname):
return IORegistryEntryCreateCFProperty(IOServiceGetMatchingService(0, IOServiceMatching("IOPlatformExpertDevice")), keyname, None, 0)
def get_hardware_uuid():
return io_key("IOPlatformUUID")
def get_hardware_serial():
return io_key("IOPlatformSerialNumber")
@erikng
Copy link
Author

erikng commented Dec 4, 2019

python 3

import objc
from Foundation import NSBundle, NSString

IOKit_bundle = NSBundle.bundleWithIdentifier_('com.apple.framework.IOKit')

functions = [("IOServiceGetMatchingService", b"II@"),
             ("IOServiceMatching", b"@*"),
             ("IORegistryEntryCreateCFProperty", b"@I@@I"),
            ]

objc.loadBundleFunctions(IOKit_bundle, globals(), functions)

def io_key(keyname):
    return IORegistryEntryCreateCFProperty(IOServiceGetMatchingService(0, IOServiceMatching("IOPlatformExpertDevice".encode("utf-8"))), NSString.stringWithString_(keyname), None, 0)

def get_hardware_uuid():
    return io_key("IOPlatformUUID")

def get_hardware_serial():
    return io_key("IOPlatformSerialNumber")

def get_board_id():
    return io_key("board-id").bytes().tobytes().decode("utf-8").rstrip("\x00")

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