Skip to content

Instantly share code, notes, and snippets.

@evanslai
Last active October 21, 2020 06:08
Show Gist options
  • Save evanslai/3711349 to your computer and use it in GitHub Desktop.
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
#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