如何获取本机MAC地址

struct ifreq ifr; 
int fd; 
fd=socket(AF_INET,SOCK_DGRAM,0); 
strcpy(ifr.ifr_name,"eth0"); 
ifr.ifr_addr.sa_family=AF_INET; 
ioctl(fd,SIOCGIFHWADDR,&ifr); 
puts(ifr.ifr_hwaddr.sa_data);

相关推荐