/* 获得网卡名 */
struct ifreq *ifr, *ifend;
struct ifconf ifc;
struct ifreq ifs[16];
int sockfd;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
ifc.ifc_len = sizeof(ifs);
ifc.ifc_req = ifs;
if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
writelog(g_log, “[error] %s(%d)-%s ioctl(): %s”,__FILE__,__LINE__,__FUNCTION__,strerror(errno) );
} else {
ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
if (ifr->ifr_addr.sa_family == AF_INET) {
if ( strncmp(ifr->ifr_name,”lo”,strlen(“lo”)) ) {
printf(“[dev]%sn”,ifr->ifr_name);
}
}
}
}
转载请注明:爱开源 » ioctl获取所有网卡信息