Linux 获取网卡信息
2015-11-25 23:02
525 查看
#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; } } }
相关文章推荐
- Kali Linux 2.0(Debian 8.0) 默认纯文本命令行启动
- arm-linux-androideabi-g++: error: CreateProcess: No such file or directory make.exe: *** [obj/local/
- apt-get update报错问题
- linux用gedit保存文件失败的解决办法
- Linux下的tar压缩解压缩命令详解
- linux下vsftpd服务器的搭建
- linux下parted创建gpt分区
- 解决linux下sublime text3无法输入中文的情况
- Linux不让显示器休眠的方法
- CentOS6 root 用户 vi/vim 无法开启高亮
- LINUX 重定向、用户和组 常用命令总结
- Ceph 单/多节点 安装小结 power by CentOS 6.x
- xmanager centos6
- Using Linux epoll with Python
- yum源的本地配置方法(对于虚拟机来说)
- Azure上Linux VM DDOS攻击预防: 慢速攻击
- 买错的电影票,含着泪也得看-LAMP搭建&Linux基础
- grep部分用法
- Linux-epoll
- Linux后台开发的要求