Uzyskaj unikalny identyfikator komputera w Pythonie w systemach windows i linux

Chciałbym uzyskać identyfikator unikalny dla komputera z Pythonem na Windows i Linux. Może to być identyfikator procesora, serial płyty głównej ... albo cokolwiek innego.

Spojrzałem na kilka modułów (pycpuid, psi, ...) bez powodzenia.

Jakiś pomysł jak to zrobić?

Author: Tshepang, 2010-03-17

11 answers

Co powiesz na użycie adresu MAC jako unikalnego identyfikatora?

Dyskusja tutaj Uzyskaj adres MAC z urządzeń korzystających z Pythona pokazuje jak uzyskać adres MAC

 0
Author: Jay Zhu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:27

Wydaje się, że nie ma bezpośredniego "pythonowego" sposobu na zrobienie tego. Na nowoczesnym sprzęcie komputerowym zazwyczaj znajduje się UUID przechowywany w BIOSie - na Linuksie znajduje się narzędzie wiersza poleceń dmidecode, które może to odczytać; przykład z mojego pulpitu:

System Information
        Manufacturer: Dell Inc.
        Product Name: OptiPlex 755                 
        Version: Not Specified
        Serial Number: 5Y8YF3J
        UUID: 44454C4C-5900-1038-8059-B5C04F46334A
        Wake-up Type: Power Switch
        SKU Number: Not Specified
        Family: Not Specified

Problem z adresami MAC polega na tym, że zwykle można łatwo zmienić je programowo (przynajmniej jeśli uruchomisz system operacyjny w maszynie wirtualnej)

W systemie Windows możesz użyć tego C API

 14
Author: Kimvais,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-17 14:23:18

Dla Windows potrzebujesz DmiDecode Dla Windows (link) :

subprocess.Popen('dmidecode.exe -s system-uuid'.split())

Dla Linuksa (non root):

subprocess.Popen('hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.uuid'.split())
 10
Author: nazca,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-30 19:13:04

Lub jeśli nie chcesz używać podprocesu, (jest powolny) użyj ctypes. To jest dla Linuksa non root.

import ctypes
from ctypes.util import find_library
from ctypes import Structure

class DBusError(Structure):
    _fields_ = [("name", ctypes.c_char_p),
                ("message", ctypes.c_char_p),
                ("dummy1", ctypes.c_int),
                ("dummy2", ctypes.c_int),
                ("dummy3", ctypes.c_int),
                ("dummy4", ctypes.c_int),
                ("dummy5", ctypes.c_int),
                ("padding1", ctypes.c_void_p),]


class HardwareUuid(object):

    def __init__(self, dbus_error=DBusError):
        self._hal = ctypes.cdll.LoadLibrary(find_library('hal'))
        self._ctx = self._hal.libhal_ctx_new()
        self._dbus_error = dbus_error()
        self._hal.dbus_error_init(ctypes.byref(self._dbus_error))
        self._conn = self._hal.dbus_bus_get(ctypes.c_int(1),
                                            ctypes.byref(self._dbus_error))
        self._hal.libhal_ctx_set_dbus_connection(self._ctx, self._conn)
        self._uuid_ = None

    def __call__(self):
        return self._uuid

    @property
    def _uuid(self):
        if not self._uuid_:
            udi = ctypes.c_char_p("/org/freedesktop/Hal/devices/computer")
            key = ctypes.c_char_p("system.hardware.uuid")
            self._hal.libhal_device_get_property_string.restype = \
                                                            ctypes.c_char_p
            self._uuid_ = self._hal.libhal_device_get_property_string(
                                self._ctx, udi, key, self._dbus_error)
        return self._uuid_

Możesz użyć tego w następujący sposób:

get_uuid = HardwareUuid()
print get_uuid()
 7
Author: Six,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-10-06 23:03:57

Wywołanie jednego z nich w powłoce lub przez rurę w Pythonie, aby uzyskać numer seryjny sprzętu komputerów Apple z systemem OS X > = 10.5:

/usr/sbin/system_profiler SPHardwareDataType | fgrep 'Serial' | awk '{print $NF}'

Lub

ioreg -l | awk '/IOPlatformSerialNumber/ { print $4 }' | sed s/\"//g

BTW: adresy MAC nie są dobrym pomysłem: w maszynie może być >1 Karta sieciowa, a adresy MAC mogą być sfałszowane .

 5
Author: Laryx Decidua,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-12 09:08:28

Myślę, że nie ma na to wiarygodnego, wieloplatformowego sposobu. Znam jedno urządzenie sieciowe, które zmienia swój adres MAC jako formę raportowania błędów sprzętowych i istnieje milion innych sposobów, aby to się nie udało.

Jedynym niezawodnym rozwiązaniem jest przypisanie aplikacji unikalnego klucza do każdej maszyny. Tak, można go sfałszować, ale nie musisz się martwić, że całkowicie się zepsuje. Jeśli martwisz się spoofingiem, możesz zastosować jakiś heurystyczny (jak zmiana w adres mac), aby spróbować ustalić, czy klucz został przeniesiony.

UPDATE: możesz użyć bakteryjnego odcisku palca .

 4
Author: mikerobi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-17 13:35:22
 3
Author: Pratik Deoghare,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-17 09:46:02

Dla python3. 6 i windows należy użyć decode

>>> import subprocess
... current_machine_id = subprocess.check_output('wmic csproduct get uuid').decode().split('\n')[1].strip()
... print(current_machine_id)
 2
Author: Alexandr Dragunkin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-26 17:53:16

Znalazłem coś innego, czego używam. Adres Mac dla Linuksa, MachineGuid Dla windows i jest też coś dla mac.

Więcej szczegółów tutaj: http://www.serialsense.com/blog/2011/02/generating-unique-machine-ids/

 1
Author: darkpotpot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-20 11:08:47

To powinno działać na windows:

import subprocess
current_machine_id = subprocess.check_output('wmic csproduct get uuid').split('\n')[1].strip()
 1
Author: Souvik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-17 09:13:14

Zabawne! Ale uuid.getnode zwraca tę samą wartość co dmidecode.exe .

subprocess.Popen('dmidecode.exe -s system-uuid'.split())

00000000-0000-0000-0000-001FD088565A

import uuid    
uuid.UUID(int=uuid.getnode())

UUID('00000000-0000-0000-0000-001fd088565a')
 0
Author: Alexandr Dragunkin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-10 13:02:27