Fixed identifier for a machine (uuid.getnode)

Viewed 11493

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.

3 Answers

uuid.getnode will return the same value for every call wthinin a single run of your app. If it has to defer to the random algorithm, then you will get a different value when you start a new instance of your app.

The implementation for getNode shows why. This is sort of what the routine looks like in python 3.7 (comments mine, code simplified for clarity)

_node = None

def getnode():

    global _node
    if _node is not None:
        # Return cached value
        return _node

    # calculate node using platform specific logic like unix functions, ifconfig, etc 
    _node = _get_node_from_platform()
    if not _node:
        # couldn't get node id from the system. Just make something up
        _node = _get_random_node() 
    return _node
Related