UUID(Universally Unique Identifier)即通用唯一标识符,是指在一台机器上生成的数字,保证在全球范围的唯一性。可用的开源库如libuuid,可参考https://blog.csdn.net/fengbingchun/article/details/94590406。

UDID(Unique Device Identifier)即设备唯一标识符。一般可通过获取设备的MAC地址+设备的CPU序列号作为设备的唯一标识符。

MAC地址(Media Access Control Address),直译为媒体访问控制地址,也称为局域网地址(LAN Address),以太网地址(Ethernet Address)或物理地址(Physical Address),它是一个用来确认网络设备位置的地址。在OSI模型中,第三层网络层负责IP地址,第二层数据链路层则负责MAC地址。MAC地址用于在网络中唯一标示一个网卡,一台设备若有一或多个网卡,则每个网卡都需要并会有一个唯一的MAC地址。

MAC地址共48位(6个字节),以十六进制表示。第1Bit为广播地址(0)/群播地址(1),第2Bit为广域地址(0)/区域地址(1)。前3~24位由IEEE决定如何分配给每一家制造商,且不重复,后24位由实际生产该网络设备的厂商自行指定且不重复。

通过命令查看MAC地址

(1). Windows:打开命令提示符(cmd.exe),运行ipconfig/all命令,执行结果如下所示:如果计算机上有多个网络设备(无论物理或虚拟),则会有多组信息及MAC地址,需辨识相应的设备。

(2). Linux:第一种方法运行ifconfig命令;第二种方法运行ip link show命令,执行结果如下所示:eth0为第一块物理网卡,HWaddr 2c:fd:a1:bc:1f:44就是MAC地址,lo为本地回环地址。

修改MAC地址:网卡MAC地址可以通过Windows设备管理员或其他工具修改。对于某些手机、平板电脑设备来说,其MAC地址/产品序号均由厂方连同销售或保修时的客户资料一并记录在案,而有关的MAC地址也不可通过常规手段来修改。

注:以上MAC地址内容主要来自 维基百科

CPU都有一个唯一的ID号,称CPUID,即CPU序列号,是在制造CPU的时候,由厂家置入到CPU内部的。但是近年的Intel CPU不再区分同一批次中各个CPU的序列号,这样就有可能两台电脑获得的CPU序列号是一样的。

通过命令查看CPU序列号

(1). Windows:打开命令提示符,运行wmic cpu get processorid命令,执行结果如下图所示:

(2). Linux:第一种方法运行dmidecode -t 4 | grep ID命令;第二种方法运行cpuid -r命令,执行结果如下图所示:

以下是代码段通过C++获取Mac地址和CPU序列号的实现:

namespace {#ifdef __linux__
// reference: https://stackoverflow.com/questions/6491566/getting-the-machine-serial-number-and-cpu-id-using-c-c-in-linux
inline void native_cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx)
{// ecx is often an input as well as an outputasm volatile("cpuid": "=a" (*eax),"=b" (*ebx),"=c" (*ecx),"=d" (*edx): "0" (*eax), "2" (*ecx));
}
#endif} // namespaceint get_mac_and_cpuid()
{// get mac
#ifdef _MSC_VER// reference: https://stackoverflow.com/questions/13646621/how-to-get-mac-address-in-windows-with-cPIP_ADAPTER_INFO AdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));if (AdapterInfo == nullptr) {fprintf(stderr, "fail to malloc\n");return -1;}DWORD dwBufLen = sizeof(IP_ADAPTER_INFO);std::unique_ptr<char[]> mac_addr(new char[18]);// Make an initial call to GetAdaptersInfo to get the necessary size into the dwBufLen variableif (GetAdaptersInfo(AdapterInfo, &dwBufLen) == ERROR_BUFFER_OVERFLOW) {free(AdapterInfo);AdapterInfo = (IP_ADAPTER_INFO *)malloc(dwBufLen);if (AdapterInfo == nullptr) {fprintf(stderr, "fail to malloc\n");return -1;}}if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == NO_ERROR) {for (PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo; pAdapterInfo != nullptr; pAdapterInfo = pAdapterInfo->Next) {// technically should look at pAdapterInfo->AddressLength and not assume it is 6.if (pAdapterInfo->AddressLength != 6) continue;if (pAdapterInfo->Type != MIB_IF_TYPE_ETHERNET) continue;sprintf(mac_addr.get(), "%02X:%02X:%02X:%02X:%02X:%02X",pAdapterInfo->Address[0], pAdapterInfo->Address[1],pAdapterInfo->Address[2], pAdapterInfo->Address[3],pAdapterInfo->Address[4], pAdapterInfo->Address[5]);fprintf(stdout, "mac address: %s\n", mac_addr.get());break;}}free(AdapterInfo);
#else// reference: https://stackoverflow.com/questions/1779715/how-to-get-mac-address-of-your-machine-using-a-c-program/35242525int sock = socket(AF_INET, SOCK_DGRAM, 0);if (sock < 0) {fprintf(stderr, "fail to socket: %d\n", sock);return -1;};struct ifconf ifc;char buf[1024];int success = 0;ifc.ifc_len = sizeof(buf);ifc.ifc_buf = buf;if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) {fprintf(stderr, "fail to ioctl: SIOCGIFCONF\n");return -1;}struct ifreq* it = ifc.ifc_req;const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));struct ifreq ifr;for (; it != end; ++it) {strcpy(ifr.ifr_name, it->ifr_name);if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {if (!(ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopbackif (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {success = 1;break;}}} else { fprintf(stderr, "fail to ioctl: SIOCGIFFLAGS\n");return -1;}}unsigned char mac_address[6];if (success) memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);fprintf(stdout, "mac address: %02X:%02X:%02X:%02X:%02X:%02X\n", mac_address[0], mac_address[1], mac_address[2], mac_address[3], mac_address[4], mac_address[5]);
#endif// Capture vendor stringchar vendor[0x20];memset(vendor, 0, sizeof(vendor));// get cpid
#ifdef _MSC_VER// reference: https://docs.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex?view=vs-2019std::array<int, 4> cpui;// Calling __cpuid with 0x0 as the function_id argument gets the number of the highest valid function ID__cpuid(cpui.data(), 0);int nIds_ = cpui[0];std::vector<std::array<int, 4>> data_;  for (int i = 0; i <= nIds_; ++i) {__cpuidex(cpui.data(), i, 0);data_.push_back(cpui);fprintf(stdout, "%08X-%08X-%08X-%08X\n", cpui[0], cpui[1], cpui[2], cpui[3]);}*reinterpret_cast<int*>(vendor) = data_[0][1];*reinterpret_cast<int*>(vendor + 4) = data_[0][3];*reinterpret_cast<int*>(vendor + 8) = data_[0][2];fprintf(stdout, "vendor: %s\n", vendor); // GenuineIntel or AuthenticAMD or otherfprintf(stdout, "vendor serialnumber: %08X%08X\n", data_[1][3], data_[1][0]);
#elseunsigned eax, ebx, ecx, edx;eax = 0; // processor info and feature bitsnative_cpuid(&eax, &ebx, &ecx, &edx);fprintf(stdout, "%d, %d, %d, %d\n", eax, ebx, ecx, edx);*reinterpret_cast<int*>(vendor) = ebx;*reinterpret_cast<int*>(vendor + 4) = edx;*reinterpret_cast<int*>(vendor + 8) = ecx;fprintf(stdout, "vendor: %s\n", vendor); // GenuineIntel or AuthenticAMD or othereax = 1; // processor serial numbernative_cpuid(&eax, &ebx, &ecx, &edx);// see the CPUID Wikipedia article on which models return the serial number in which registersprintf("vendor serialnumber: %08X%08X\n", edx, eax);
#endifreturn 0;
}

Windows下执行结果如下所示:与命令行执行结果相同

Linux上执行结果如下图所示:与命令行执行结果相同

GitHub:https://github.com//fengbingchun/Messy_Test

Windows/Linux获取Mac地址和CPU序列号实现相关推荐

  1. php获取主板序列号,PHP获取通过windows系统命令wmic获取MAC地址、CPU序列号、主板序列号...

    在项目中,客户需要系统在win系统上获取MAC地址.CPU序列号和主板序列号等,在网上搜索下,通过windows系统命令wmic可以获取,测试基本可行,HardwareInfo.php源代码如下: $ ...

  2. python wmi读取网卡MAC地址、CPU序列号、硬盘序列号、主板序列号、BIOS序列号

    序列号相当于电脑的身份证号,是硬件出厂时,厂商写在硬件里的唯一识别码,具有唯一性和不可修改性.很多正版软件以此来识别用户电脑,限制安装. import uuid import wmi def get_ ...

  3. 通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

    开发语言:C/C++ 支持平台:Windows 实现功能: 通过WMI获取网卡MAC地址.硬盘序列号.主板序列号.CPU ID.BIOS序列号 下载地址: WMI_DeviceQuery.zip 版本 ...

  4. java获取操作系统的MAC地址和硬盘序列号

    1.判断操作系统是Windows还是Linux private static Boolean isLinux() {String os = System.getProperty("os.na ...

  5. linux用c++获取mac地址,网卡地址,网口地址,网卡序号ip地址,不使用 ioctl(sock, SIOCGIFCONF, ifc)获取网络接口名称,这个接口有时会返回-1获取不到,换方法获取

    linux用c++获取mac地址,不使用 ioctl(sock, SIOCGIFCONF, &ifc)获取网络接口名称,这个接口有时会返回-1获取不到,换方法获取 1.弃用 SIOCGIFCO ...

  6. linux c 获取mac地址吗,Linux系统下用C语言获取MAC地址

    最近在做一个小程序,需要用到在linux系统里编写C程序从而获取MAC地址,从网上搜了一遍,想总结一下.如果你就只需要单个功能的程序,可以采用方法一,见代码1,一般最好能够封装起来,写成获取MAC地址 ...

  7. 获取MAC地址的四种方法(转)

    https://www.cnblogs.com/zlshmily/p/10058560.html zlshmily 在实际的应用系统中,我们往往会需要在程序运行时获取当前机器的网卡的MAC地址,以便作 ...

  8. Python根据IP地址获取MAC地址

    Python3根据IP地址获取MAC地址(不能获取本机IP,可以获取与本机同局域网设备IP的MAC) main.py #!/usr/bin/env python3 # -*- coding: utf- ...

  9. python获取mac地址_你知道怎么用Python获取计算机名,ip地址,mac地址吗

    获取计算机名 # 获取计算机名,常用的方法有三种,但最常用的是第一种 import os import socket # method one name = socket.gethostname() ...

最新文章

  1. Windows 8部署系列PART6:准备模板计算机配置
  2. 解决问题 inner element must either be a resource reference or empty.
  3. CMM (软件工程与集成产品开发)
  4. Python语言的全部数据类型分享!
  5. 拿着锤子找钉子,数字芯片领导者比特大陆进军人工智能
  6. Linux服务器iops性能测试-fio
  7. STM32H743+Keil-将变量定义到指定内存
  8. JavaScript 基础知识 - DOM篇(二)
  9. js radio 获值
  10. 数学之路(3)-机器学习(3)-机器学习算法-欧氏距离(3)
  11. 如何在 Mac 上设置和使用快捷方式?
  12. Photoshop CS2序列号大全 官方免费密钥
  13. html星空代码在线,怎么操作html星空特效代码
  14. python打印九九乘法表代码
  15. 太赞了! 豆瓣9.3分的《Linux 命令行大全》.pdf 限时下载
  16. 会员运营是什么?会员运营体系有哪些类别?
  17. 看aps高级排产如何实现生产计划智能排产
  18. strcpy和strncpy区别
  19. Spring Data ElasticSearch 3.2版本发布,相关新特性说明
  20. 雅可比行列式_二重积分换元法、雅可比行列式

热门文章

  1. 平均数编码:针对高基数定性特征(类别特征)的数据预处理/特征工程
  2. invalid non-printable character U+200D
  3. 华为钱包扫码云闪付_支持华为钱包云闪付的有几个机型
  4. Web前端开发 北京林业大学 CSS样式-单元作业
  5. RTOS内功修炼记(十) | 深度解析RTOS内核上下文切换机制
  6. 基于YOLOV3的通用物体检测项目实战---(5)利用DarkNet框架进行YOLOV3模型训练实操(笔记)
  7. 彻底的理解:WebService到底是什么?
  8. Fragment的Tag
  9. CET-6--2018.12--1
  10. 一文读懂什么是CTO、技术VP、技术总监、首席架构师