-
-
Save Hirunagrad/874f98e5ed4f3122382fd697de2b5e26 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