如何用C获得MAC Address?
发表于 : 2007-03-11 1:03
如题
谢谢!
谢谢!
代码: 全选
system("ifconfig eth0 | grep ""硬件地址"" |awk '{print $4 }'");
代码: 全选
main()
{
char fname[256],cmd[256],buf[256];
FILE *f;
tmpnam(fname);
sprintf(cmd,"ifconfig eth0 | grep ""硬件地址"" |awk '{print $4 }'>%s",fname);
printf("正在获取MAC地址...\n");
system(cmd);
system("ifconfig eth0 | grep ""硬件地址"" |awk '{print $4 }'");
f =fopen(fname,"r");
while(fgets(buf,sizeof buf,f))
printf("%s",buf);
fclose(f);
unlink(fname);
}
代码: 全选
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/if_ether.h>
int main(int argc, char **argv)
{
int nSocket;
struct ifreq struReq;
nSocket = socket(PF_INET,SOCK_STREAM,0);
memset(&struReq,0,sizeof(struReq));
if(argc < 2)
strncpy(struReq.ifr_name, "eth0", sizeof(struReq.ifr_name));
else
strncpy(struReq.ifr_name, argv[1], sizeof(struReq.ifr_name));
ioctl(nSocket,SIOCGIFHWADDR,&struReq);
fprintf(stderr, "%d-%s\n", __LINE__, strerror(errno));
printf("%s\n", ether_ntoa(struReq.ifr_hwaddr.sa_data));
close(nSocket);
}