机器的固定标识符(uuid.getnode) [英] Fixed identifier for a machine (uuid.getnode)

查看:1828
本文介绍了机器的固定标识符(uuid.getnode)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试寻找一些可以用作脚本的唯一字符串/数字的东西,这些脚本/数字固定在机器上并且易于获取(跨平台).我想一台机器上会有网卡.我不需要它真的是唯一的,但是有必要将它从长远来看并尽可能地少用.

I'm trying to find something I can use as a unique string/number for my script that is fixed in a machine and easily obtainable(cross-platform). I presume a machine would have a network card. I don't need it to be really unique, but the necessary is it should be fixed in a long run and as rare as possible.

我知道MAC可以更改,并且我可能会在脚本中对其进行警告,但是我不希望任何人每天早上都更改MAC.

I know MAC can be changed and I'd probably make a warning about it in my script, however I don't expect anyone to change MAC each morning.

我想出的是uuid.getnode(),但是在 docs 中有:

What I came up with is uuid.getnode(), but in the docs there is:

如果所有尝试获取硬件地址的尝试均失败,我们将选择一个随机的48位数字

If all attempts to obtain the hardware address fail, we choose a random 48-bit number

这是否意味着对于每个函数调用,我都会获得另一个随机数,因此如果无法获得MAC,就不可能使用它?

Does it mean that for each function call I get another random number, therefore it's not possible to use it if MAC is unobtainable?

...在具有多个网络接口的计算机上,可能会返回其中任何一个的MAC地址.

...on a machine with multiple network interfaces the MAC address of any one of them may be returned.

这句话是否意味着getnode()从所有可用的地址中获得随机(或第一个)MAC?如果它在第一次运行中获得MAC A并在下次获得MAC B会怎样?如果我得到一个固定的列表(排序,连接,tadaaa!)就没有问题了

Does this sentence mean getnode() gets a random(or first) MAC from all available? What if it'd get MAC A in first run and MAC B next time? There'd be no problem if I'd get a fixed list(sort, concatenate, tadaaa!)

我问是因为我没有办法自己测试.

I'm asking because I have no way how to test it myself.

推荐答案

我设法在我的android设备上测试了第一部分,并且在每个新的python运行中,它创建了随机数,因此完全不能用于此目的.

I managed to test the first part on my android device and on each new python run it created random number, so it's not usable at all for this purpose.

第二种问题淹没了自己,因为如果在文档中提到它可能返回any one of them,那么您就不能依靠它了(+我找不到可以对其进行测试的机器).一个不错的软件包netifaces来了,它做了类似的事情

The second problem kind of drowned itself, because if in the docs it mentioned that it may return any one of them, then it's not something you could rely on (+I couldn't find a machine I could test it on). A nice package netifaces came to rescue, which does a similar thing

netifaces.interfaces() # returns e.g. ['lo', 'eth0', 'tun2']

netifaces.ifaddresses('eth0')[netifaces.AF_LINK]
# returns [{'addr': '08:00:27:50:f2:51', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]

但是我宁愿放弃使用MAC,也得到了更稳定的东西.

However I rather gave up using MACs, I got something rather more stable.

现在有标识符:

1)Windows:

执行这一步并获得输出可能就足够了:

Executing this one and getting output may be good enough:

wmic csproduct get UUID

或我使用过的一个,可以在注册表(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography)中找到:

or the one I used and is available in registry (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography):

import _winreg
registry = _winreg.HKEY_LOCAL_MACHINE
address = 'SOFTWARE\\Microsoft\\Cryptography'
keyargs = _winreg.KEY_READ | _winreg.KEY_WOW64_64KEY
key = _winreg.OpenKey(registry, address, 0, keyargs)
value = _winreg.QueryValueEx(key, 'MachineGuid')
_winreg.CloseKey(key)
unique = value[0]

2)Linux:

/sys/class/dmi/id/board_serial

/sys/class/dmi/id/product_uuid

或(如果不是root的话):

or if not root:

cat /var/lib/dbus/machine-id

3)Android:

如果您使用的是python,并且不想弄乱Java的东西,那么它应该可以很好地工作:

If you are working with python and don't want to mess with Java stuff, then this should work pretty good:

import subprocess
cmd = ['getprop', 'ril.serialnumber']
self.unique = subprocess.check_output(cmd)[:-1]

但是,如果您喜欢Java,则可以使用此答案,即使ANDROID_ID的独特性也值得商if可以更改它,因此序列号最有可能是更安全的选择.

but if you like Java, then go for this answer although even ANDROID_ID's uniqueness is rather debatable if it's allowed to change, therefore a serial number is most likely a safer bet.

请注意,就像链接答案中已经提到的那样,即使ril.serialnumber也可以为空/空或不存在(缺少键).即使使用官方的 Android API ,同样的事情也会发生这么说:

Note that like it's already mentioned in the linked answer, even ril.serialnumber can be null/empty or non-existing (missing key). Same thing happens even with the official Android API where it's clearly stated this:

硬件序列号,(如果可用).

Mac/iPhone: 我找不到任何解决方案,因为我无法访问任何这些解决方案,但是如果有一个保存机器ID值的变量,那么您应该可以使用简单的subprocess.check_output()

Mac/iPhone: I couldn't find any solution as I don't have access to any of these, but if there is a variable that holds the machine id value, then you should be able to get there with simple subprocess.check_output()

这篇关于机器的固定标识符(uuid.getnode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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