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;
}

Comments are closed.

Related URL:
  1. [好文推薦] A TUTORIAL ON POINTERS AND ARRAYS IN C
  2. The Expat XML Parser – XML 分析/拆解工具 – C – 這個讚! 用 C 寫 spider 會用到
  3. Protected: Some Simple C Programs – 好用的 C 範例程式/片段
  4. 新的轉碼方法 , libiconv , BIG5 轉 UTF8
  5. get my ip address useing c / sample code
  6. 解決 error while loading shared libraries 的方法
  7. compile multiple source code