如何从 C/C++ 中的 MAC 地址获取供应商 OUI 和设备名称

How to get the vendor OUI and Device name from MAC Address in C/C++

我正在 libtins 库的帮助下用 C++ 开发网络扫描器,我可以获得 MAC 地址和 IP,但我想进一步了解供应商(例如:Intel Corporate)和 C++ 中的设备名称(例如:DESKTOP-TO5P0BD)


获取 Mac 和 IP

的代码

// 检索 ARP 层信息

const ARP& arp = pdu.rfind_pdu<ARP>();
std::cout << "Found :" << arp.sender_ip_addr() << ", " << arp.sender_hw_addr() << std::endl;
// Checking if it is an ARP reply?
if (arp.opcode() == ARP::REPLY) {
    // Let's check if there's already an entry for this address
auto iter = addresses.find(arp.sender_ip_addr());
if (iter == addresses.end()) {
    std::cout << "saving " << arp.sender_ip_addr() << ", " << arp.sender_hw_addr() << std::endl;
    // We haven't seen this address. Save it.
    addresses.insert({ arp.sender_ip_addr(), arp.sender_hw_addr() });
    IPv4Address ip = arp.sender_ip_addr();
    NetworkInterface iface(ip);
    //std::cout << iface.name() << std::endl;
}
else {
    std::cout << "already seen " << arp.sender_ip_addr() << ", " << arp.sender_hw_addr() << std::endl;
    // We've seen this address. If it's not the same HW address, inform it
    if (arp.sender_hw_addr() != iter->second) {
        std::cout << "[WARNING] " << arp.sender_ip_addr() << " is at "
            << iter->second << " but also at " << arp.sender_hw_addr()
            << std::endl;
    }
}
}

为了从MAC地址获取vendor,你可以看看this MAC OUI vendor database mantained by Wireshark。这是一个格式简单的文本文件。

为了得到“设备名称”,你可以做一个NetBIOS name lookup. This Whosebug question可能对你有帮助。

如果您想要 MAC 中的供应商名称,您可以通过阅读 arp -a (probably via the winapi). Next you need to search a vendor db, the wireshark list is good, there's also this one. As for the "Device Name", you could check this with WMI. Other than the more complex winapi for this, you could use a library like this one 来获取 MAC,这要简单得多。您会需要向 Win32_ComputerSystem class 发出请求,其中包含设备名称和型号等。MAC 地址也可以通过 WMI 检索,而不是查询 Win32_NetworkAdapter - 它提供了所有接口,所以一定要找到正确的接口!