Home  >  Q&A  >  body text

c++ - mac系统,纯c语言怎样获取网卡信息?

mac系统,纯c语言怎样获取网卡信息?

黄舟黄舟2765 days ago589

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-17 14:32:44

    Excerpted from the code in zabbix, I don’t know if it is enough, please refer to it first

    static int    get_ifmib_general(const char *if_name)
    {
        int    mib[6], ifcount;
        size_t    len;
    
        if (NULL == if_name || 'rrreee'== *if_name)
            return SYSINFO_RET_FAIL;
    
        mib[0] = CTL_NET;
        mib[1] = PF_LINK;
        mib[2] = NETLINK_GENERIC;
        mib[3] = IFMIB_SYSTEM;
        mib[4] = IFMIB_IFCOUNT;
    
        len = sizeof(ifcount);
    
        if (-1 == sysctl(mib, 5, &ifcount, &len, NULL, 0))
            return FAIL;
    
        mib[3] = IFMIB_IFDATA;
        mib[5] = IFDATA_GENERAL;
    
        len = sizeof(ifmd);
    
        for (mib[4] = 1; mib[4] <= ifcount; mib[4]++)
        {
            if (-1 == sysctl(mib, 6, &ifmd, &len, NULL, 0))
            {
                if (ENOENT == errno)
                    continue;
    
                break;
            }
    
            if (0 == strcmp(ifmd.ifmd_name, if_name))
                return SUCCEED;
        }
    
        return FAIL;
    }

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 14:32:44

    The top command is written by reading the information in the /proc file system.
    /proc/net/dev and /proc/net/snmp can obtain interface and other fields.
    If you want to obtain hardware device information, you can use system() to call the awk and lspci commands to obtain the manufacturer model information.

    reply
    0
  • Cancelreply