Linux下C语言获取所有网卡MAC地址的代码
2017-04-20 21:52
2839 查看
Linux下C语言获取所有网卡MAC地址的代码
原文:http://www.open-open.com/code/view/1432215294567
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
struct ifreq ifr;
struct ifconf ifc;
char buf[2048];
int success = 0;
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock == -1) {
printf("socket error\n");
return -1;
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) {
printf("ioctl error\n");
return -1;
}
struct ifreq* it = ifc.ifc_req;
const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
char szMac[64];
int count = 0;
for (; it != end; ++it) {
strcpy(ifr.ifr_name, it->ifr_name);
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
count ++ ;
unsigned char * ptr ;
ptr = (unsigned char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
snprintf(szMac,64,"%02X:%02X:%02X:%02X:%02X:%02X",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
printf("%d,Interface name : %s , Mac address : %s \n",count,ifr.ifr_name,szMac);
}
}
}else{
printf("get mac info error\n");
return -1;
}
}
}
执行结果如下:
1,Interface name : eth0 , Mac address : 90:B1:1D:87:13:75
2,Interface name : virtac , Mac address : 52:54:00:96:E0:BF
3,Interface name : map , Mac address : 52:54:00:F2:FE:9C
4,Interface name : poi , Mac address : 52:54:00:1B:97:82
5,Interface name : vnet1 , Mac address : 52:54:00:05:14:E3
原文:http://www.open-open.com/code/view/1432215294567
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
struct ifreq ifr;
struct ifconf ifc;
char buf[2048];
int success = 0;
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock == -1) {
printf("socket error\n");
return -1;
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) {
printf("ioctl error\n");
return -1;
}
struct ifreq* it = ifc.ifc_req;
const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
char szMac[64];
int count = 0;
for (; it != end; ++it) {
strcpy(ifr.ifr_name, it->ifr_name);
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
count ++ ;
unsigned char * ptr ;
ptr = (unsigned char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
snprintf(szMac,64,"%02X:%02X:%02X:%02X:%02X:%02X",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
printf("%d,Interface name : %s , Mac address : %s \n",count,ifr.ifr_name,szMac);
}
}
}else{
printf("get mac info error\n");
return -1;
}
}
}
执行结果如下:
1,Interface name : eth0 , Mac address : 90:B1:1D:87:13:75
2,Interface name : virtac , Mac address : 52:54:00:96:E0:BF
3,Interface name : map , Mac address : 52:54:00:F2:FE:9C
4,Interface name : poi , Mac address : 52:54:00:1B:97:82
5,Interface name : vnet1 , Mac address : 52:54:00:05:14:E3
相关文章推荐
- linux下获取所有网卡mac地址(C语言)
- 获取硬盘ID信息及网卡MAC地址的代码
- 用纯Java代码根据IP获取windows和linux的MAC物理地址
- Python获取本机所有网卡ip,掩码和广播地址实例代码
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
- Linux 接口地址的获取-暨获取本地IP地址(所有地址,包括IPv4,IPV6,MAC 地址)
- C语言获取本地所有网卡的ip地址及MAC信息
- LINUX下QT与C语言通过网卡名获取网卡IP与MAC
- linux 下获取所有网卡mac,name
- 获取linux系统电脑网卡mac地址
- linux 设置网络API --- 开/关网卡、设置/获取Mac地址
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
- VB获取所有网卡MAC地址
- 嵌入式 获取linux下的网卡的MAC地址
- VC学习资料收集(25):获取硬盘ID信息及网卡MAC地址的代码
- java 获取本机的所有网卡的Mac地址
- JS获取客户端网卡物理地址(MAC) 代码
- linux 下C实现获取网卡IP和MAC地址
- Linux 接口地址的获取-暨获取本地IP地址(所有地址,包括IPv4,IPV6,MAC 地址)
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)