Skip to content

Instantly share code, notes, and snippets.

@Hirunagrad
Forked from evanslai/c-get_eth_mac.c
Created October 21, 2020 06:08
Show Gist options
  • Save Hirunagrad/874f98e5ed4f3122382fd697de2b5e26 to your computer and use it in GitHub Desktop.
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
#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