简体   繁体   中英

Get Machine id of MAC OS X

I want machine unique id such as processor id, hdd id, uuid of MAC PC through c++ program. Can anyone please tell me how it implements? Thanks.

only about 7 years later, but here's an answer to those stumbling across this that we've been using.

  1. It uses the IOPlatformExpertDevice class to access the Mac Serial number/hardware uuid
  2. There are two ways to do this, the first uses C++, the second python. I have personally used the second way, and can verify it fetches the hardware uuid as given by System Information.

First method, not tested by myself, but uses the same class so has at least the potential to work, see https://gist.github.com/tmiz/1294978 for a routine in C++ on how to retrieve the "serial number" which is not be the same as the hardware uuid from system information, but from some tweaking, you should be able to get the hardware uuid.

Second method (see python code below), in python, which uses the ioreg command, which is executed via a separate process, then the results processed with a regular expression to get the uuid. This method definitely retrieves the hardware uuid as I've checked it with the System Information app in macos 10.14 and previous versions of 10.13 and 10.12.

May these methods serve you well, they do not return the mac address and as such should function well as a uuid for the machine, not just the network interface.

Finally you can read about ioreg here -> http://www.manpagez.com/man/8/ioreg/ and the I/O Kit more generally here -> https://developer.apple.com/library/archive/documentation/DeviceDrivers/Conceptual/IOKitFundamentals/Families_Ref/Families_Ref.html#//apple_ref/doc/uid/TP0000021-BABHIGFE

import platform, re, os

os_type = platform.system()

if os_type == 'Darwin':

    machine_uuid_str = ''

    p = os.popen('ioreg -rd1 -c IOPlatformExpertDevice | grep -E \'(UUID)\'', "r")

    while 1:
        line = p.readline()
        if not line: break
        machine_uuid_str += line

    match_obj = re.compile('[A-Z,0-9]{8,8}-' +\
                       '[A-Z,0-9]{4,4}-' +\
                       '[A-Z,0-9]{4,4}-' +\
                       '[A-Z,0-9]{4,4}-' +\
                       '[A-Z,0-9]{12,12}')

    results = match_obj.findall(machine_uuid_str)

    return results[0]

Outside of a few ancient processors, x86 CPUs do not have software-visible serial numbers.

Apple recommends that you use the MAC address of the computer's primary network interface (ie, the onboard Ethernet controller if present, or the wireless interface otherwise) as a unique identifier for the system. Sample code for doing this is available in Apple's Validating Mac App Store Receipts documentation (under "Get the Computer's GUID").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM