如何获取MAC地址
/* get the hardware address of the interface */
int sockfd;
struct ifreq req;
unsigned char hwaddr[ETH_ALEN];
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fputs("socket error!/n", stderr);
exit (1);
}
bcopy("eth0", req.ifr_name, IFNAMSIZ);
if (ioctl(sockfd, SIOCGIFHWADDR, &req) < 0) {
fputs("Ioctl error!/n", stderr);
exit(1);
}
bcopy(((struct sockaddr *)&req.ifr_hwaddr)->sa_data, hwaddr, ETH_ALEN);
close(sockfd);
/* Print the MAC address in xx:xx:xx:xx:xx:xx format */
printf("%02x:%02x:%02x:%02x:%02x:%02x/n", *hwaddr, *(hwaddr+1),
*(hwaddr+2), *(hwaddr+3), *(hwaddr+4), *(hwaddr+5));
转自水木