登录社区云,与社区用户共同成长
邀请您加入社区
#include #include #include #include #include #include #include #include #include using namespace std;void peek_interfaces(int fd);void print_hw_addr(int fd, const char* if_
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <net/if.h> #include <stdio.h> #include <string> #include <iostream> #include <cstring> using namespace std; void peek_interfaces(int fd); void print_hw_addr(int fd, const char* if_name); int main() { int fd = socket(AF_INET, SOCK_DGRAM, 0); if(-1 == fd) { perror("Failed create socket."); return -1; } peek_interfaces(fd); close(fd); return 0; } void peek_interfaces(int fd) { ifreq ifs[16] = {0}; ifconf conf = {sizeof(ifs)}; conf.ifc_req = ifs; if(-1 == ioctl(fd, SIOCGIFCONF, &conf)) { perror("Failed IOCTL SIOCGIFCONF."); return; } if(conf.ifc_len >= sizeof(ifs)) { perror("Buffer too small for IOCTL SIOCGIFCONF."); return; } int num = conf.ifc_len / sizeof(ifreq); cout << num << " interface entry retrieved." << endl; for(int i = 0; i < num; ++i) { cout << "[ " << ifs[i].ifr_name << " ]" << endl; sockaddr_in* sai = (sockaddr_in*)&ifs[i].ifr_addr; cout << "Addr: " << inet_ntoa(sai->sin_addr) << endl; print_hw_addr(fd, ifs[i].ifr_name); cout << endl; } } void print_hw_addr(int fd, const char* if_name) { ifreq req = {0}; strcpy(req.ifr_name, if_name); if(-1 == ioctl(fd, SIOCGIFFLAGS, &req)) { perror("Failed IOCTL SIOCGIFFLAGS."); return; } if(req.ifr_flags & IFF_LOOPBACK) { cout << "Is LOOPBACK." << endl; return; } if(-1 == ioctl(fd, SIOCGIFHWADDR, &req)) { perror("Failed IOCTL SIOCGIFHWADDR."); return; } unsigned char* puc = (unsigned char*)req.ifr_hwaddr.sa_data; printf("HW addr: %02x:%02x:%02x:%02x:%02x:%02x/n", puc[0], puc[1], puc[2], puc[3], puc[4], puc[5]); }
更多推荐
网卡速率和双工模式的配置
http://linux.chinaitlab.com/system/792187.html1、mii-tool 配置网络设备协商方式的工具; 1.1 mii-tool 介绍; mii-tool - view, manipulate media-independent interface status (mii-tool 是查看,管理介质的网络接口的状态)
Linux虚拟文件系统之文件系统卸载(sys_umount())
Linux中卸载文件系统由umount系统调用实现,入口函数为sys_umount()。较于文件系统的安装较为简单,下面是具体的实现。1. /*sys_umont系统调用*/2. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)3. {4.struct path path;
Linux系统下超级终端Minicom的使用方法(例如:连接交换机,路由器等)转http://baike.baidu.com/view/2911642.htm?fr=ala0_1
<br /> Linux系统下超级终端Minicom的使用方法 <br /> Linux下的Minicom的功能与下的超级终端功能相似,适于在通过超级终端对设备的管理以及对嵌入操作系统的升级,现写出Minicom的使用手册: <br /> 1. 启动minicom <br /> 以root权限登录系统 <br /> 使用命令 <br /> minicom –s 则minicom启动,屏
扫一扫分享内容
所有评论(0)