Linux编程获取指定网口MAC地址
在Linux下,我们可以使用C语言编程获取指定网口的MAC地址,下面是相应的源码(可将eth0改为相应的网络结点):
unsigned char *ptr;
struct ifreq ifr;
int sock;
strcpy(ifr.ifr_name, "eth0");
strcpy(ifr.ifr_hwaddr.sa_data, "");
sock = socket(AF_INET, SOCK_STREAM, 0);
ioctl(sock, SIOCGIFHWADDR, &ifr);
ptr = (unsigned char *) ifr.ifr_hwaddr.sa_data;
printf("x:x:x:x:x:x\n",
(ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
(ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377));
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void main()
{
}
评论
发表评论