获取主机MAC地址
时间:2006-10-18 来源:fytzzh
获取主机MAC地址的代码:
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <linux/if_ether.h>
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]);
}
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <linux/if_ether.h>
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]);
}
相关阅读 更多 +