Getting a unique hardware ID with Python
Asked Answered
C

7

18

I have a process that requires me to identify different machines, and I'm not sure what's the best way to do it. I do not want to save that ID on a text file or something, but I want to generate it from hardware every time I need it (in case the text with the ID gets deleted or something)

I've checked UUID, and it seems ok but I'm not sure. I've taken a look at uuid.getNode(), but I have 2 problems with it:

  1. One part says "If all attempts to obtain the hardware address fail, we choose a random 48-bit number with its eighth bit set to 1 as recommended in RFC 4122", which means that I may get a different unique on some systems for some reason - is there a way to identify which time it failed and generate something else?

  2. another part says: " “Hardware address” means the MAC address of a network interface, and on a machine with multiple network interfaces the MAC address of any one of them may be returned.", which means if i have 2 different network adapters, each call I may get any one of them? that's not good for me.

If you have a better way of obtaining a unique ID for a machine, that I can generate each time and won't have to worry about deletion of it or something - I'd be glad to hear it. all of my attempts to find something have failed. Thanks.

Calcicole answered 12/7, 2016 at 11:53 Comment(2)
Duplicate question #2461641Blouse
This is a duplicate question to this: #2461641 Check this: https://mcmap.net/q/46639/-get-a-unique-computer-id-in-python-on-windows-and-linuxTranslate
I
11

You could use dmidecode.

Linux:

import subprocess

def get_id():
    return subprocess.Popen('hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.uuid'.split())

Windows:
NOTE: Requires dmidecode for Windows

import subprocess

def get_id():
    return subprocess.Popen('dmidecode.exe -s system-uuid'.split())

Cross-platform:
NOTE: Requires dmidecode for Windows

import subprocess
import os

def get_id():
    if 'nt' in os.name:
        return subprocess.Popen('dmidecode.exe -s system-uuid'.split())
    else:
        return subprocess.Popen('hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.uuid'.split())
Infringement answered 12/7, 2016 at 12:19 Comment(2)
Thank you! that was very helpful. using dmidecode is an excellent idea. (didn't know this)Calcicole
Linux update 2020: HAL is ancient Linux history. Use dmidecode -s system-uuid instead.Elegiac
M
21

Please note that you can get the same UUID from Windows without installing any additional software with the following command:

C:\> wmic csproduct get uuid
Maurist answered 10/10, 2016 at 11:40 Comment(6)
Use subprocess to run it from your python code: import subprocess current_machine_id = subprocess.check_output('wmic csproduct get uid').split('\n')[1].strip()Translate
CalledProcessError: Command 'wmic csproduct get uid' returned non-zero exit status -2147217385Nalepka
Thats a very good answer and even a better comment from SouvikGoebel
Small typo in Souvik comment. Use import subprocess current_machine_id = subprocess.check_output('wmic csproduct get uuid').split('\n')[1].strip() (uuid instead of uid) :-)Chromic
Fixing typo from @tim. current_machine_id = subprocess.check_output('wmic csproduct get uuid').split(b'\n')[1].strip() You have to split a byte with a byte.Modie
python 3 : subprocess.check_output('wmic csproduct get uuid').decode("utf-8").split('\n')[1].strip()Simp
I
11

You could use dmidecode.

Linux:

import subprocess

def get_id():
    return subprocess.Popen('hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.uuid'.split())

Windows:
NOTE: Requires dmidecode for Windows

import subprocess

def get_id():
    return subprocess.Popen('dmidecode.exe -s system-uuid'.split())

Cross-platform:
NOTE: Requires dmidecode for Windows

import subprocess
import os

def get_id():
    if 'nt' in os.name:
        return subprocess.Popen('dmidecode.exe -s system-uuid'.split())
    else:
        return subprocess.Popen('hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.uuid'.split())
Infringement answered 12/7, 2016 at 12:19 Comment(2)
Thank you! that was very helpful. using dmidecode is an excellent idea. (didn't know this)Calcicole
Linux update 2020: HAL is ancient Linux history. Use dmidecode -s system-uuid instead.Elegiac
C
4

For windows this seems to get same uuid every time por each device based on the MAC address:

str(uuid.uuid1(uuid.getnode(),0))[24:]

But it does not seem to keep same ID on Android 4.4.2.

Conjunctive answered 16/10, 2016 at 20:55 Comment(1)
for some reason on the same machine but different python versions str(uuid.uuid1(uuid.getnode(),0)) returns different 8 first letters and the others the same, be aware if you want to use the full UUID and use different python versions.Cuckoo
I
3

After seeing this question asked quite a few times both here on SO as well as in support requests for my software licensing business (called Keygen), I wrote a small, cross-platform PyPI package that queries a machine's native GUID called machineid.

Essentially, it looks like this, but with some Windows-specific WMI registry queries for more a accurate ID. The package also has support for hashing the ID, to anonymize it.

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')
Iridosmine answered 13/10, 2022 at 15:16 Comment(0)
C
2

This worked for me:

import subprocess

current_machine_id = str(subprocess.check_output('wmic csproduct get uuid'), 'utf-8').split('\n')[1].strip()

print(current_machine_id)
Conspiracy answered 5/4, 2021 at 13:33 Comment(0)
G
1

The ideal approach which I resorted to was this. It is quite fast and efficient.

hwid = str(subprocess.check_output(
    'wmic csproduct get uuid')).split('\\r\\n')[1].strip('\\r').strip()
data = requests.get(
    'https://gist.githubusercontent.com/rishav394/z/raw/x')
if hwid in data.text:
    print('Authenticated!')
    auth = True
else:
    print(hwid + ' was not found on the server.\nNot authorised!')
Goebel answered 22/4, 2019 at 17:26 Comment(1)
Note that hwid is just a name. It is NOT the Hardware ID of your machine.Goebel
O
-1

or use bios serialnr

wmic bios get serialnumber
Osyth answered 26/3, 2019 at 7:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.