pythonhardwareuuididentifiermac-address

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 uuid.getnode(), 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 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.


Solution

  • 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']
    
    netifaces.ifaddresses('eth0')[netifaces.AF_LINK]
    # 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')
    _winreg.CloseKey(key)
    unique = value[0]
    

    2) Linux:

    /sys/class/dmi/id/board_serial
    

    or

    /sys/class/dmi/id/product_uuid
    

    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 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.

    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:

    A hardware serial number, if available.

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