您的位置:首页 > 其它

获取本机(所有网卡)IP地址(IPV4)

2015-05-19 21:33 429 查看

Linux C语言 获取本机(所有网卡)IP地址(IPV4)

1、根据ioctl机制打印当前所有网卡代码:#include <sys/ioctl.h>#include <net/if.h>#include <netinet/in.h>#include <arpa/inet.h>#include <string.h>#include <stdio.h>
int get_local_ip(char *ip){ int fd, intrface, retn = 0; struct ifreq buf[INET_ADDRSTRLEN]; struct ifconf ifc; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) { ifc.ifc_len = sizeof(buf); // caddr_t,linux内核源码里定义的:typedef void *caddr_t; ifc.ifc_buf = (caddr_t)buf; if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc)) { intrface = ifc.ifc_len/sizeof(struct ifreq); while (intrface-- > 0) { if (!(ioctl(fd, SIOCGIFADDR, (char *)&buf[intrface]))) { ip=(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr)); printf("IP:%s\n", ip); } } } close(fd); return 0; }}int main(){ char ip[64]; memset(ip, 0, sizeof(ip)); get_local_ip(ip); return 0;}编译:[root@localhost get_ip]# gcc -Wall -g ip.c [root@localhost get_ip]# ./a.out IP:192.168.2.53IP:192.168.2.55IP:127.0.0.1代码解析:<netinet/in.h>中定义了 #define INET_ADDRSTRLEN 16ifreq、ifconf 、ioctl参考:http://blog.csdn.net/wl_haanel/article/details/4536236
2、通过枚举网卡打印当前所有网卡代码:#include <stdio.h>#include <ifaddrs.h>#include <netinet/in.h>#include <string.h>#include <arpa/inet.h>
int get_local_ip(char *ip) { struct ifaddrs *ifAddrStruct; void *tmpAddrPtr=NULL; getifaddrs(&ifAddrStruct); while (ifAddrStruct != NULL) { if (ifAddrStruct->ifa_addr->sa_family==AF_INET) { tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr; inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN); printf("%s IP Address:%s\n", ifAddrStruct->ifa_name, ip); } ifAddrStruct=ifAddrStruct->ifa_next; } //free ifaddrs freeifaddrs(ifAddrStruct); return 0;}
int main(){ char ip[16]; memset(ip, 0, sizeof(ip)); get_local_ip(ip); return 0;}编译:[root@localhost get_ip]# gcc -Wall -g ip.c [root@localhost get_ip]# ./a.out lo IP Address:127.0.0.1eth0 IP Address:192.168.2.55eth1 IP Address:192.168.2.53代码解析:API接口可查看man 7 netdevice
3、多网卡情况下,将所有IP地址以字符串形式打印出,用逗号(“,”)隔开,形式如:“127.0.0.1,192.168.2.55,192.168.2.53”代码:#include <stdio.h>#include <ifaddrs.h>#include <netinet/in.h>#include <string.h>#include <arpa/inet.h>
int get_local_ip(char *ips) { struct ifaddrs *ifAddrStruct; void *tmpAddrPtr=NULL; char ip[INET_ADDRSTRLEN]; int n = 0; getifaddrs(&ifAddrStruct); while (ifAddrStruct != NULL) { if (ifAddrStruct->ifa_addr->sa_family==AF_INET) { tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr; inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN); printf("%s IP Address:%s\n", ifAddrStruct->ifa_name, ip); if (n == 0){ strncat(ips, ip, INET_ADDRSTRLEN); } else { strncat(ips, ",", 1); strncat(ips, ip, INET_ADDRSTRLEN); } n++; } ifAddrStruct=ifAddrStruct->ifa_next; } //free ifaddrs freeifaddrs(ifAddrStruct); return 0;}int main(){ char ip[64]; memset(ip, 0, sizeof(ip)); get_local_ip(ip); printf("IP:%s\n", ip); return 0;}编译:[root@localhost get_ip]# gcc -Wall -g ip.c [root@localhost get_ip]# ./a.out lo IP Address:127.0.0.1eth0 IP Address:192.168.2.55eth1 IP Address:192.168.2.53IP:127.0.0.1,192.168.2.55,192.168.2.53
4、多网卡情况下,将多个ip地址放入字符串数组中(去掉"127.0.0.1"地址)代码:#include <stdio.h>#include <ifaddrs.h>#include <netinet/in.h>#include <string.h>#include <arpa/inet.h>
int get_local_ip(char *ip_list) { struct ifaddrs *ifAddrStruct; void *tmpAddrPtr; char ip[INET_ADDRSTRLEN]; int n = 0; getifaddrs(&ifAddrStruct); while (ifAddrStruct != NULL) { if (ifAddrStruct->ifa_addr->sa_family==AF_INET) { tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr; inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN); if (strcmp(ip, "127.0.0.1") != 0) {// printf("%s IP Address:%s\n", ifAddrStruct->ifa_name, ip); if (n == 0){ memcpy(ip_list, ip, INET_ADDRSTRLEN); } else { memcpy(ip_list+INET_ADDRSTRLEN, ip, INET_ADDRSTRLEN); } n++; } } ifAddrStruct=ifAddrStruct->ifa_next; } //free ifaddrs freeifaddrs(ifAddrStruct); return n;}int main(){ char ip[3][INET_ADDRSTRLEN]; memset(ip, 0, sizeof(ip)); int n; for (n=get_local_ip(*ip); n>0; n--) { printf("IP:%s\n", ip[n-1]); } return 0;}编译:[root@localhost get_ip]# gcc -Wall -g ip.c [root@localhost get_ip]# ./a.out IP:192.168.2.53IP:192.168.2.55http://blog.csdn.net/ruiyiin/article/details/25830351
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: