简体   繁体   English

获取MAC OS X的机器ID

[英]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. 我想要机器唯一的ID,例如处理器ID,HDD ID,通过C ++程序的MAC PC的uuid。 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. 仅在大约7年后,但这是对那些我们一直在使用的绊脚石的答案。

  1. It uses the IOPlatformExpertDevice class to access the Mac Serial number/hardware uuid 它使用IOPlatformExpertDevice类访问Mac序列号/硬件uuid
  2. There are two ways to do this, the first uses C++, the second python. 有两种方法可以做到这一点,第一种使用C ++,第二种使用python。 I have personally used the second way, and can verify it fetches the hardware uuid as given by System Information. 我亲自使用了第二种方法,并且可以验证它是否已获取系统信息所提供的硬件uuid。

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. 第一种方法,未​​经我自己测试,但使用相同的类,因此至少具有工作的潜力,有关如何检索“序列号”的C ++例程,请参见https://gist.github.com/tmiz/1294978它与系统信息中的硬件uuid不同,但是通过一些调整,您应该可以获取硬件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. 第二种方法(请参见下面的python代码)在python中,它使用ioreg命令,该命令是通过单独的进程执行的,然后使用正则表达式处理结果以获取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. 正如我在macOS 10.14中的“系统信息”应用程序以及10.13和10.12的早期版本中检查的那样,此方法肯定会检索硬件uuid。

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. 也许这些方法对您有用,它们不会返回mac地址,因此应该可以很好地用作计算机的uuid,而不仅仅是网络接口。

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 最后,您可以在这里-> http://www.manpagez.com/man/8/ioreg/以及有关I / O Kit的更多信息中了解ioreg-> https://developer.apple.com/library/archive/文档/ DeviceDrivers /概念/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. 在一些古老的处理器之外,x86 CPU没有软件可见的序列号。

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. Apple建议您使用计算机主要网络接口(即,板载以太网控制器(如果存在)或无线接口,否则为无线接口)的MAC地址作为系统的唯一标识符。 Sample code for doing this is available in Apple's Validating Mac App Store Receipts documentation (under "Get the Computer's GUID"). Apple的“ 验证Mac App Store收据”文档(在“获取计算机的GUID”下)中提供了执行此操作的示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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