小编use*_*626的帖子

所有接口的C/C++ Linux MAC地址

我使用以下代码检索当前计算机的所有MAC地址:

ifreq ifr;
ifconf ifc;
char buf[1024];

int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock == -1) { ... };

ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) { ... }

ifreq *it = ifc.ifc_req;
const ifreq* const end = it + (ifc.ifc_len / sizeof(ifreq));

for (; it != end; ++it) {
    strcpy(ifr.ifr_name, it->ifr_name);
    if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
        if (!(ifr.ifr_flags & IFF_LOOPBACK)) {
            if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
               unsigned char mac_address[6]; …
Run Code Online (Sandbox Code Playgroud)

c linux ioctl mac-address ifconfig

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×1

ifconfig ×1

ioctl ×1

linux ×1

mac-address ×1