使用Python获取唯一的硬件ID [英] Getting a unique hardware ID with Python

查看:1219
本文介绍了使用Python获取唯一的硬件ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个过程需要我识别不同的机器,我不确定什么是最好的方法. 我不想将该ID保存在文本文件或其他内容中,但是我想在每次需要时从硬件生成该ID(以防带有ID的文本被删除或其他内容)

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)

我已经检查了 UUID ,看来还可以,但是我没有把握. 我看过 uuid.getNode(),但它有2个问题:

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. 其中一部分说:如果所有尝试获取硬件地址的尝试均失败,我们将按照RFC 4122的建议,选择一个随机的48位数字,将其第八位设置为1",这意味着我可能会有所不同出于某些原因在某些系统上是唯一的-是否有办法确定它在哪个时间失败并生成其他内容?

  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?

另一部分说:硬件地址"是指网络接口的MAC地址,在具有多个网络接口的计算机上,可能会返回其中任何一个的MAC地址."有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.

如果您有更好的方法来获取机器的唯一ID,则我可以每次生成该ID,而不必担心会删除它或其他东西-我很高兴听到它.我所有尝试找到东西的尝试都以失败告终. 谢谢.

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.

推荐答案

您可以使用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:
注意:对于Windows,需要 dmidecode

Windows:
NOTE: Requires dmidecode for Windows

import subprocess

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

跨平台:
注意:对于Windows,需要 dmidecode

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

这篇关于使用Python获取唯一的硬件ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆