Getting a unique hardware ID with Python

為{幸葍}努か 提交于 2020-03-17 09:38:29

问题


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.


回答1:


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())



回答2:


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



回答3:


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.




回答4:


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!')



回答5:


or use bios serialnr

wmic bios get serialnumber


来源:https://stackoverflow.com/questions/38328176/getting-a-unique-hardware-id-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!