Skip to content

Instantly share code, notes, and snippets.

@n8felton
Forked from pudquick/nvram.py
Last active December 7, 2021 00:27
Show Gist options
  • Save n8felton/649f66baf51c2c4c33d46b8c25433a7f to your computer and use it in GitHub Desktop.
Save n8felton/649f66baf51c2c4c33d46b8c25433a7f to your computer and use it in GitHub Desktop.
Get nvram values via python and pyobjc on macOS
import objc
from Foundation import NSBundle
IOKit_bundle = NSBundle.bundleWithIdentifier_('com.apple.framework.IOKit')
functions = [
("IORegistryEntryFromPath", b"II*"),
("IORegistryEntryCreateCFProperty", b"@I@@I"),
]
objc.loadBundleFunctions(IOKit_bundle, globals(), functions)
def nvram(keyname):
raw = IORegistryEntryCreateCFProperty(IORegistryEntryFromPath(0, b"IODeviceTree:/options"), keyname, None, 0)
# This returns the raw bytes
# For string values, this will be what you're looking for
# For more complex/structured values, you may need to parse the bytes
return raw.bytes().tobytes()
# example usage:
# Look up a macOS device serial number via nvram
def serialnumber():
return nvram("4D1EDE05-38C7-4A6A-9CC6-4BCCA8B38C14:SSN").decode('utf-8').strip('\0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment