- 论坛徽章:
- 0
|
linux下如何用c取得指定网卡的mac地址?
- #include <stdio.h>;
- #include <unistd.h>;
- #include <string.h>;
- #include <sys/ioctl.h>;
- #include <sys/socket.h>;
- #include <net/if.h>;
- int main(){
- int sockfd, i;
- struct ifreq buffer;
-
- sockfd = socket(PF_INET,SOCK_DGRAM,0);
- if (sockfd <= 0) {
- fputs("Can not create socket!\n", stderr);
- return 1;
- }
- memset(&buffer, 0, sizeof(struct ifreq));
- strcpy(buffer.ifr_name, "eth0");
- ioctl(sockfd, SIOCGIFHWADDR, &buffer);
- close(sockfd);
- fputs("Network MAC address: ", stdout);
- for(i = 0; i < 6; i++)
- printf("%.2x:", (unsigned char)buffer.ifr_hwaddr.sa_data[i]);
- putchar('\n');
- return 0;
- }
复制代码 |
|