当前位置: IT大杂烩 > Ubuntu  > linux c获取mac

linux c获取mac

www.someabcd.com  网友分享于:Jun 8, 2018 5:50:49 PM

标签:

#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <errno.h>

char *get_local_mac(char *if_name){
    struct ifreq m_ifreq;
    int sock = 0;
    char mac[32] = " ";

    sock = socket(AF_INET,SOCK_STREAM,0);
    strcpy(m_ifreq.ifr_name,if_name);

    ioctl(sock,SIOCGIFHWADDR,&m_ifreq);
    int i = 0;
    for(i = 0; i < 6; i++){
        sprintf(mac+3*i, "%02X:", (unsigned char) m_ifreq.ifr_hwaddr.sa_data[i]);
    }
    mac[strlen(mac) - 1] = 0;
    printf("MAC: %s\n", mac);

    return NULL;
}


int main(){
    get_local_mac("eth0");
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

linux c获取mac

标签:

发布此文章仅为传递网友分享,不代表本站观点,若侵权请联系我们删除,本站将不对此承担任何责任。
Copyright ©2018  IT大杂烩  版权所有  京ICP备11030978号-1 网站地图