1

I'm looking for python equivalent library/module for node-machine-id

I want to uniquely identify each desktop/device & get that UUID & send to DB. I'm able to achieve this particular thing in javascript with below code

import pkg from 'node-machine-id';
const {machineIdSync} = pkg;
let id = machineIdSync();
console.log(id)

or

let ida = machineIdSync({original: true})
console.log(ida)

I'm aware that python has inbuilt uuid module but that ID is variable, I want only unique ID of the computer the way I was able to do with node-machine-id Thank you!

  • it seems page [node-machine-id](https://www.npmjs.com/package/node-machine-id) explains what it uses to create machine ID - for different systems it use different values but you could use the same values in Python. For example for Linux it reads from file `/var/lib/dbus/machine-id` - so in python it will be `open('/var/lib/dbus/machine-id').read()` – furas Mar 18 '22 at 23:23
  • [source code](https://github.com/automation-stack/node-machine-id/blob/master/index.js) shows how it gets ID. It uses `exec()` to execute external programs. Code is not long so you could rewrite it in Python. It only execute external program and later it clean some values. – furas Mar 18 '22 at 23:27

1 Answers1

0

I wrote a small, cross-platform PyPI package that queries a machine's native GUID called machineid. It's very similar to the node-machine-id Node package.

Essentially, it looks like the code blow, but with some Windows-specific WMI registry queries for an even more accurate ID on Windows machines.

The package also has support for anonymizing the ID via hashing.

import subprocess
import sys

def run(cmd):
  try:
    return subprocess.run(cmd, shell=True, capture_output=True, check=True, encoding="utf-8") \
                     .stdout \
                     .strip()
  except:
    return None

def guid():
  if sys.platform == 'darwin':
    return run(
      "ioreg -d2 -c IOPlatformExpertDevice | awk -F\\\" '/IOPlatformUUID/{print $(NF-1)}'",
    )

  if sys.platform == 'win32' or sys.platform == 'cygwin' or sys.platform == 'msys':
    return run('wmic csproduct get uuid').split('\n')[2] \
                                         .strip()

  if sys.platform.startswith('linux'):
    return run('cat /var/lib/dbus/machine-id') or \
           run('cat /etc/machine-id')

  if sys.platform.startswith('openbsd') or sys.platform.startswith('freebsd'):
    return run('cat /etc/hostid') or \
           run('kenv -q smbios.system.uuid')

Open to feedback and PRs!

ezekg
  • 924
  • 10
  • 20