获取主机MAC地址的代码:
#include
#include
#include
#include
#include
#include
#include
int main()
{
struct ifreq ifr;
int sock;
unsigned char *ptr;
int status;
strcpy (ifr.ifr_name , "eth0");
sock = socket (PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sock == -1)
{
perror ("socket error ");
return -1;
}
status = ioctl (sock, SIOCGIFHWADDR, &ifr);
if (status < 0)
{
perror ("ioctl error ");
close (sock);
return -2;
}
ptr = ifr.ifr_hwaddr.sa_data;
printf("%02x.%02x.%02x.%02x.%02x.%02x\n",
ptr[0], ptr[1],ptr[2],ptr[3],ptr[4],ptr[5]);
}
阅读(1456) | 评论(1) | 转发(0) |