Linux作業系統獲取網路卡初始的MAC地址

LuiseDalian發表於2014-04-30

問題描述:修改過網路卡原始的MAC地址,現在想恢復網路卡出廠時原始的MAC地址

問題解決:編譯執行下面的C程式即可。

#include

#include

#include

#include

#include

#include

#include

 

int main()

{

    int fd;

    struct ifreq ifr;

    fd = socket(AF_INET, SOCK_DGRAM, 0);

 

    ifr.ifr_addr.sa_family = AF_INET;

    strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);

    ioctl(fd, SIOCGIFHWADDR, &ifr);

    close(fd);

 

    printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",

          (unsigned char)ifr.ifr_hwaddr.sa_data[0],

          (unsigned char)ifr.ifr_hwaddr.sa_data[1],

          (unsigned char)ifr.ifr_hwaddr.sa_data[2],

          (unsigned char)ifr.ifr_hwaddr.sa_data[3],

          (unsigned char)ifr.ifr_hwaddr.sa_data[4],

          (unsigned char)ifr.ifr_hwaddr.sa_data[5]);

 

  return 0;

}

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17013648/viewspace-1151904/,如需轉載,請註明出處,否則將追究法律責任。

相關文章