linux系统下通过代码获取网口MAC地址

首先ifconfig命令行查看所有网口详细信息【包括各个网口的MAC地址】

由于我linux系统有几个网口,所有列举了2个例子

获取wlan MAC地址

void  CenterGetLanMac(Char *ver)
{
    FILE    *verPtr;
    Char    lineStr[512];

    strcpy(ver,"00:00:00:00:00:00");
    verPtr = popen("cat /sys/class/net/eth0/address","r");

    if(verPtr != NULL)
    {
        if(fgets(lineStr,512,verPtr) != NULL)         //读取一行信息
        {
            strcpy(ver,lineStr);
        }
        pclose(verPtr);
    }
}

获取wifi网口MAC地址

void  CenterGetWlanMac(Char *ver)
{
    FILE    *verPtr;
    Char    lineStr[512];

    strcpy(ver,"00:00:00:00:00:00");
    verPtr = popen("cat /sys/class/net/ra0/address","r");
    if(verPtr != NULL)
    {
        if(fgets(lineStr,512,verPtr) != NULL)         //读取一行信息
        {
            strcpy(ver,lineStr);
        }
        pclose(verPtr);
    }
}

 

你可能感兴趣的:(Linux)