Last active
October 21, 2020 06:08
-
-
Save evanslai/3711349 to your computer and use it in GitHub Desktop.
C: How to get MAC address of your machine using a C program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/socket.h> | |
#include <sys/ioctl.h> | |
#include <linux/if.h> | |
#include <netdb.h> | |
#include <stdio.h> | |
#include <string.h> | |
int main() | |
{ | |
struct ifreq s; | |
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); | |
strcpy(s.ifr_name, "eth0"); | |
if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) { | |
int i; | |
for (i = 0; i < 6; ++i) | |
printf(" %02x", (unsigned char) s.ifr_addr.sa_data[i]); | |
puts("\n"); | |
return 0; | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment