69,382
社区成员
发帖
与我相关
我的任务
分享
$ cat /etc/udev/rules.d/70-persistent-net.rules
# This file maintains persistent names for network interfaces.
# See udev(7) for syntax.
#
# Entries are automatically added by the 75-persistent-net-generator.rules
# file; however you are also free to add your own entries.
# PCI device 0x10ec:0x8168 (r8169)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:26:18:a2:16:eb", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# PCI device 0x168c:0x001c (ath5k)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:24:2c:ad:70:ae", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan0"
wlan0 Link encap:Ethernet HWaddr 00:24:2c:ad:70:ae
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
struct ifreq ifreq;
int sockfd;
unsigned char buffer_mac[6];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){/*创建套接口,后用于获取mac地址*/
perror("sockfd");
return -1;
}
strcpy(ifreq.ifr_name,"eth0"); /*获取一号以太网卡的物理地址*/
if(ioctl(sockfd,SIOCGIFHWADDR,&ifreq)<0){/*获取mac地址*/
perror("ioctl");
return -2;
}
memcpy(buffer_mac,ifreq.ifr_hwaddr.sa_data,6);/*复制mac地址到buffer_mac*/
printf("MAC:%02x:%02x:%02x:%02x:%02x:%02x\n",buffer_mac[0],buffer_mac[1],buffer_mac[2],buffer_mac[3],buffer_mac[4],buffer_mac[5]);
close(sockfd); /*关闭套接口*/
return 0;
}