Categories
Copy_N_Paste JOB Programming/C System/Linux/Unix*

Get MAC address using C

不過 , 這邊有指定 eth0 , 若 server 有很多 interface 的話就要注意一下了.

#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>

int main( int argc, char *argv[] )
{
	int s;
	struct ifreq buffer;

	s = socket(PF_INET, SOCK_DGRAM, 0);

	memset(&buffer, 0x00, sizeof(buffer));
	
	strcpy(buffer.ifr_name, "eth0");

	ioctl(s, SIOCGIFHWADDR, &buffer);

	close(s);
	
	for( s = 0; s < 6; s++ )
	{
		printf("%.2X ", (unsigned char)buffer.ifr_hwaddr.sa_data[s]);
	}

	printf("\n");

	return 0;
}