(I googled for this)
Code: Select all
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/if.h>
int main()
{
struct ifreq ifr;
struct ifreq *IFR;
struct ifconf ifc;
char buf[1024];
int s, i;
if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return -1;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
ioctl(s, SIOCGIFCONF, &ifc);
IFR = ifc.ifc_req;
for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++) {
strcpy(ifr.ifr_name, IFR->ifr_name);
if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
if (! (ifr.ifr_flags & IFF_LOOPBACK)) {
if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
int j;
for (j=0; j<5; ++j)
printf("%02x:", ((unsigned char*)ifr.ifr_hwaddr.sa_data)[j]);
printf("%02x\n", ((unsigned char*)ifr.ifr_hwaddr.sa_data)[j]);
}
}
}
}
close(s);
return 0;
}