c語言 獲取mac地址
阿新 • • 發佈:2019-01-25
#include #include #include #include #include #include int main(int argc, char *argv[]){ int i; /* get mac */ struct ifreq ifreq; int sock; char mac[32]; if((sock=socket(AF_INET,SOCK_STREAM,0))<0) { perror("error"); return 2; } strcpy(ifreq.ifr_name,argv[2]);
if(ioctl(sock,SIOCGIFHWADDR,&ifreq)<0) { perror("error:"); return 3; }/* printf("%02x%02x%02x%02x%02x%02x ", (unsigned char)ifreq.ifr_hwaddr.sa_data[0], (unsigned char)ifreq.ifr_hwaddr.sa_data[1], (unsigned char)ifreq.ifr_hwaddr.sa_data[2], (unsigned char)ifreq.ifr_hwaddr.sa_data[3],
(unsigned char)ifreq.ifr_hwaddr.sa_data[4], (unsigned char)ifreq.ifr_hwaddr.sa_data[5]);*/ for (i=0; i<6; i++) sprintf(mac+2*i, "%02x", (unsigned char)ifreq.ifr_hwaddr.sa_data[i]); printf("MAC: %s ", mac); return 0;}