Archive

Posts Tagged ‘fingerprint’

Fingerprint of the operating system/platform

January 30, 2013 Leave a comment

Problem
I have a script that I want to run in different environments: on Linux, on Windows, on my home machine, at my workplace, in virtualbox, etc. In each environment I want to use different configurations. For instance the temp. directory on Linux would be /tmp, on Windows c:\temp, etc. When the script starts, I want to test the environment and load the corresponding configuration settings. How to get an OS-independent fingerprint of the environment?

Solution
I came up with the following solution. For my purposes the short fingerprint is enough.

import platform as p
import uuid
import hashlib

def get_fingerprint(md5=False):
    """
    Fingerprint of the current operating system/platform.
    If md5 is True, a digital fingerprint is returned.
    """
    sb = []
    sb.append(p.node())
    sb.append(p.architecture()[0])
    sb.append(p.architecture()[1])
    sb.append(p.machine())
    sb.append(p.processor())
    sb.append(p.system())
    sb.append(str(uuid.getnode())) # MAC address
    text = '#'.join(sb)
    if md5:
        md5 = hashlib.md5()
        md5.update(text)
        return md5.hexdigest()
    else:
        return text

def get_short_fingerprint(length=6):
    """
    A short digital fingerprint of the current operating system/platform.
    Length should be at least 6 characters.
    """
    assert 6 <= length <= 32
    #
    return get_fingerprint(md5=True)[-length:]

The up-to-date version of the source is available here as part of my jabbapylib library.

Design a site like this with WordPress.com
Get started