linux用C语言读取网卡MAC地址
想获取硬盘和CPU的序列号,看了hdparm源码,编译后运行,可是没能获取到硬盘的序列号,那就试试读网卡的MAC地址了,这是在网上找到的代码。
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
struct ifreq ifreq;
int sock;
if(argc!=2)
{
printf("Usage : ethname\n");
return 1;
}
if((sock=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket");
return 2;
}
strcpy(ifreq.ifr_name,argv[1]);
if(ioctl(sock,SIOCGIFHWADDR,&ifreq)<0)
{
perror("ioctl");
return 3;
}
printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
(unsigned char)ifreq.ifr_hwaddr.sa_data[0],
(unsigned char)ifreq.ifr_hwaddr.sa_data[1],
(unsigned char)ifreq.ifr_hwaddr.sa_data[2],
(unsigned char)ifreq.ifr_hwaddr.sa_data[3],
(unsigned char)ifreq.ifr_hwaddr.sa_data[4],
(unsigned char)ifreq.ifr_hwaddr.sa_data[5]);
return 0;
}
阅读(746) | 评论(0) | 转发(0) |