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.53

IP:192.168.2.55

IP:127.0.0.1

代码解析:

<netinet/in.h>中定义了 #define INET_ADDRSTRLEN 16

ifreq、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.1

eth0 IP Address:192.168.2.55

eth1 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.1

eth0 IP Address:192.168.2.55

eth1 IP Address:192.168.2.53

IP: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.53

IP:192.168.2.55

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值