Skip to content

Instantly share code, notes, and snippets.

@freyes
Created December 16, 2021 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freyes/db746c860104a8d260f4398209abad40 to your computer and use it in GitHub Desktop.
Save freyes/db746c860104a8d260f4398209abad40 to your computer and use it in GitHub Desktop.
def _get_machine_id():
"""Return the machine-id UUID."""
with open('/etc/machine-id', 'r') as f:
# The machine ID is a single newline-terminated, hexadecimal,
# 32-character, lowercase ID.
machine_id_int = int(f.read().strip(), 16)
machine_id_bytes = bytearray(machine_id_int.to_bytes(16, 'big'))
# see manpage machine-id(5)
machine_id_bytes[6] = (machine_id_bytes[6] & 0x0F) | 0x40
machine_id_bytes[8] = (machine_id_bytes[8] & 0x3F) | 0x80
machine_uuid = uuid.UUID(bytes=bytes(machine_id_bytes))
return str(machine_uuid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment