KeyWeeUsr KeyWeeUsr - 4 months ago 30x
Python Question

Fixed identifier for a machine (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.

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.

What I came up with is

, but in the docs there is:

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

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?

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

Does this sentence mean
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.


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.

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']

# returns [{'addr': '08:00:27:50:f2:51', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]

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

Now to the identifiers:

1) Windows:

Executing this one and getting output may be good enough:

wmic csproduct get UUID

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')
unique = value[0]

2) Linux:




or if not root:

cat /var/lib/dbus/machine-id

3) Android:

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]

but if you like Java, then go for this answer

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