Linux下获取MAC地址

#include
#include
#include
#include
#include


int get_mac(char* mac)
{
 
    struct ifreq tmp;
 
    int sock_mac;
 
    char mac_addr[50] = {0};
 
    sock_mac = socket(AF_INET, SOCK_STREAM, 0);
 
    if( sock_mac == -1){
 
        perror("create socket fail\n");
 
        return -1;
 
    }
 
    memset(&tmp,0,sizeof(tmp));
 
    strncpy(tmp.ifr_name,"eth0",sizeof(tmp.ifr_name)-1 );
 
    if( (ioctl( sock_mac, SIOCGIFHWADDR, &tmp)) < 0 ){
 
        printf("mac ioctl error\n");
 
        return -1;
 
    }
 
    sprintf(mac_addr, "%02x:%02x:%02x:%02x:%02x:%02x",
 
            (unsigned char)tmp.ifr_hwaddr.sa_data[0],
 
            (unsigned char)tmp.ifr_hwaddr.sa_data[1],
 
            (unsigned char)tmp.ifr_hwaddr.sa_data[2],
 
            (unsigned char)tmp.ifr_hwaddr.sa_data[3],
 
            (unsigned char)tmp.ifr_hwaddr.sa_data[4],
 
            (unsigned char)tmp.ifr_hwaddr.sa_data[5]
 
            );
 
    
    close(sock_mac);
 
    memcpy(mac,mac_addr,strlen(mac_addr));
 
    return 0;
 
}

你可能感兴趣的:(C/C++)