当前位置: 技术问答>linux和unix
!我如何才能编程获得网卡的MAC地址?
来源: 互联网 发布时间:2015-10-07
本文导语: 用Socket可以获得IP地址,在C++程序里用什么办法可以提取出来访问我服务器的各个client的MAC地址呢? | /*用C语言得到本机的硬件地址*/ #include #include #include #include #include #include #include #include#defi...
用Socket可以获得IP地址,在C++程序里用什么办法可以提取出来访问我服务器的各个client的MAC地址呢?
|
/*用C语言得到本机的硬件地址*/
#include
#include
#include
#include
#include
#include
#include
#include#defineMAXINTERFACES16intmain(argc,argv)
registerintargc;
registerchar*argv[];
{
register int fd,intrface,retn=0;
struct ifreq buf[MAXINTERFACES];
struct arpreq arp;
struct ifconf ifc;if((fd=socket(AF_INET,SOCK_DGRAM,0))>=0){
ifc.ifc_len=sizeof(buf);
ifc.ifc_buf=(caddr_t)buf;
if(!ioctl(fd,SIOCGIFCONF,(char*)&ifc)){
intrface=ifc.ifc_len/sizeof(structifreq);
printf("interfacenumisintrface=%dnnn",intrface);
while(intrface-->0){
printf("netdevice%sn",buf[intrface].ifr_name);/*Jugdewhetherthenetcardstatusispromisc*/
if(!(ioctl(fd,SIOCGIFFLAGS,(char*)&buf[intrface]))){
if(buf[intrface].ifr_flags&IFF_PROMISC){
puts("theinterfaceisPROMISC");
retn++;
}
}else{
charstr[256];sprintf(str,"cpm:ioctldevice%s",
buf[intrface].ifr_name);
perror(str);
}/*Jugdewhetherthenetcardstatusisup*/
if(buf[intrface].ifr_flags&IFF_UP){
puts("theinterfacestatusisUP");
}else{
puts("theinterfacestatusisDOWN");
}/*GetIPofthenetcard*/
if(!(ioctl(fd,SIOCGIFADDR,(char*)&buf[intrface]))){
puts("IPaddressis:");
puts(inet_ntoa
(((structsockaddr_in*)(&buf[intrface].
ifr_addr))->sin_addr));
puts("");
//puts(buf[intrface].ifr_addr.sa_data);
}else{
charstr[256];sprintf(str,"cpm:ioctldevice%s",
buf[intrface].ifr_name);
perror(str);
}/*GetHWADDRESSofthenetcard*/
if(!(ioctl(fd,SIOCGIFHWADDR,(char*)&buf[intrface]))){
puts("HWaddressis:");printf("%02x:%02x:%02x:%02x:%02x:%02xn",
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[0],
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[1],
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[2],
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[3],
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[4],
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[5]);puts("");
puts("");
}else{
charstr[256];sprintf(str,"cpm:ioctldevice%s",
buf[intrface].ifr_name);
perror(str);
}
}
}else
perror("cpm:ioctl");}else
perror("cpm:socket");close(fd);
return;
}
试试这个看行不,别的地方找的,我还没试过。
#include
#include
#include
#include
#include
#include
#include
#include#defineMAXINTERFACES16intmain(argc,argv)
registerintargc;
registerchar*argv[];
{
register int fd,intrface,retn=0;
struct ifreq buf[MAXINTERFACES];
struct arpreq arp;
struct ifconf ifc;if((fd=socket(AF_INET,SOCK_DGRAM,0))>=0){
ifc.ifc_len=sizeof(buf);
ifc.ifc_buf=(caddr_t)buf;
if(!ioctl(fd,SIOCGIFCONF,(char*)&ifc)){
intrface=ifc.ifc_len/sizeof(structifreq);
printf("interfacenumisintrface=%dnnn",intrface);
while(intrface-->0){
printf("netdevice%sn",buf[intrface].ifr_name);/*Jugdewhetherthenetcardstatusispromisc*/
if(!(ioctl(fd,SIOCGIFFLAGS,(char*)&buf[intrface]))){
if(buf[intrface].ifr_flags&IFF_PROMISC){
puts("theinterfaceisPROMISC");
retn++;
}
}else{
charstr[256];sprintf(str,"cpm:ioctldevice%s",
buf[intrface].ifr_name);
perror(str);
}/*Jugdewhetherthenetcardstatusisup*/
if(buf[intrface].ifr_flags&IFF_UP){
puts("theinterfacestatusisUP");
}else{
puts("theinterfacestatusisDOWN");
}/*GetIPofthenetcard*/
if(!(ioctl(fd,SIOCGIFADDR,(char*)&buf[intrface]))){
puts("IPaddressis:");
puts(inet_ntoa
(((structsockaddr_in*)(&buf[intrface].
ifr_addr))->sin_addr));
puts("");
//puts(buf[intrface].ifr_addr.sa_data);
}else{
charstr[256];sprintf(str,"cpm:ioctldevice%s",
buf[intrface].ifr_name);
perror(str);
}/*GetHWADDRESSofthenetcard*/
if(!(ioctl(fd,SIOCGIFHWADDR,(char*)&buf[intrface]))){
puts("HWaddressis:");printf("%02x:%02x:%02x:%02x:%02x:%02xn",
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[0],
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[1],
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[2],
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[3],
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[4],
(unsigned char)buf[intrface].ifr_hwaddr.
sa_data[5]);puts("");
puts("");
}else{
charstr[256];sprintf(str,"cpm:ioctldevice%s",
buf[intrface].ifr_name);
perror(str);
}
}
}else
perror("cpm:ioctl");}else
perror("cpm:socket");close(fd);
return;
}
试试这个看行不,别的地方找的,我还没试过。
|
包含net/if_arp.h,其他的自己到if_arp.h里面去看吧……
|
使用ioctl函数了。
论坛随便一搜很多了。
论坛随便一搜很多了。