Skip to content

Instantly share code, notes, and snippets.

@jc0b
Created February 7, 2023 19:20
Show Gist options
  • Save jc0b/7989a63b03672ac377665bc5abc6e7d0 to your computer and use it in GitHub Desktop.
Save jc0b/7989a63b03672ac377665bc5abc6e7d0 to your computer and use it in GitHub Desktop.
Get the boardID/deviceID of a Mac.
import subprocess
def get_board_id():
"""Gets the local device ID on Apple Silicon Macs or the board_id of older Macs"""
ioreg_cmd = ["/usr/sbin/ioreg", "-c", "IOPlatformExpertDevice", "-d", "2"]
try:
ioreg_output = subprocess.check_output(ioreg_cmd).splitlines()
board_id = ""
device_id = ""
for line in ioreg_output:
line_decoded = line.decode("utf8")
if "board-id" in line_decoded:
board_id = line_decoded.split("<")[-1]
board_id = board_id[
board_id.find('<"') + 2 : board_id.find('">') # noqa: E203
]
elif "compatible" in line_decoded:
device_details = line_decoded.split("<")[-1]
device_details = device_details[
device_details.find("<")
+ 2 : device_details.find(">") # noqa: E203
]
device_id = (
device_details.replace('","', ";").replace('"', "").split(";")[0]
)
if board_id:
return board_id
elif device_id:
return device_id
except subprocess.CalledProcessError as err:
raise ReplicationError(err)
print(get_board_id())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment