linux的IP及虚IP(附加IP)获取打印

如下为C++代码

cpp 复制代码
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <unistd.h> // Add this header for 'close' function
#include <stdio.h>
#define MAX_IFS_NUM      64
#define MAC_ADDR_LEN     6
#define IP_ADDR_LEN      4

struct netif_info{
    char ifname[IFNAMSIZ];
    char ifreal[IFNAMSIZ];
    unsigned char macaddr[IFHWADDRLEN];
    int ifindex;
    in_addr_t ip;
    in_addr_t mask;
    short int flags;
};

int GetNetifList(netif_info netifs[MAX_IFS_NUM], int &num) {
    int ret = 0;
    int sockfd;
    struct ifconf ifc;
    struct ifreq  ifs[MAX_IFS_NUM];
    struct ifreq  ifr;
    struct sockaddr_in *addr;
    int nsize;

    num = 0;
    sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (sockfd == -1)
        return -1;
    ifc.ifc_len = sizeof(ifs);
    ifc.ifc_req = ifs;
    if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
        ret = -2;
        goto exit;
    }
    nsize = ifc.ifc_len / sizeof(struct ifreq);
    if (nsize > MAX_IFS_NUM) {
        ret = -3;
        goto exit;
    }
    for (int n = 0; n < nsize; ++n) {
        ifr = ifc.ifc_req[n];
        strncpy(netifs[num].ifname, ifr.ifr_name, IFNAMSIZ);
        strncpy(netifs[num].ifreal, ifr.ifr_name, IFNAMSIZ); // Assuming ifname and ifreal are the same initially

        if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
            ret = -7;
            goto exit;
        }
        netifs[num].flags = ifr.ifr_flags;

        // Get IP address
        if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0) {
            netifs[num].ip = 0;
        } else {
            addr = (struct sockaddr_in*)&ifr.ifr_addr;
            netifs[num].ip = addr->sin_addr.s_addr;
        }

        // Get subnet mask
        if (ioctl(sockfd, SIOCGIFNETMASK, &ifr) < 0) {
            netifs[num].mask = 0;
        } else {
            addr = (struct sockaddr_in*)&ifr.ifr_netmask;
            netifs[num].mask = addr->sin_addr.s_addr;
        }

        if (ioctl(sockfd, SIOCGIFINDEX, &ifr) < 0) {
            ret = -5;
            goto exit;
        }
        netifs[num].ifindex = ifr.ifr_ifindex;

        if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
            ret = -8;
        } else {
            memcpy(netifs[num].macaddr, ifr.ifr_hwaddr.sa_data, MAC_ADDR_LEN);
        }

        ++num;
    }

exit:
    close(sockfd);
    return ret;
}

int main() {
    int num;
    netif_info netifs[MAX_IFS_NUM];
    int ret = GetNetifList(netifs, num);
    if (ret != 0) {
        std::cerr << "Failed to get network interface list. Error code: " << ret << std::endl;
        return ret;
    }

    std::cout << "Number of network interfaces: " << num << std::endl;
    std::cout << "---------------------------------" << std::endl;

    for (int i = 0; i < num; ++i) {
        std::cout << "Interface Name: " << netifs[i].ifname << std::endl;

        std::cout << "IP Address: ";
        for (int j = 0; j < IP_ADDR_LEN; ++j) {
            std::cout << ((netifs[i].ip >> (j * 8)) & 0xFF);
            if (j < IP_ADDR_LEN - 1) {
                std::cout << ".";
            }
        }
        std::cout << std::endl;

        std::cout << "Subnet Mask: ";
        for (int j = 0; j < IP_ADDR_LEN; ++j) {
            std::cout << ((netifs[i].mask >> (j * 8)) & 0xFF);
            if (j < IP_ADDR_LEN - 1) {
                std::cout << ".";
            }
        }
        std::cout << std::endl;

        std::cout << "Interface Index: " << netifs[i].ifindex << std::endl;

        std::cout << "MAC Address: ";
        for (int j = 0; j < MAC_ADDR_LEN; ++j) {
            printf("%02X", netifs[i].macaddr[j]);
            if (j < MAC_ADDR_LEN - 1) {
                printf(":");
            }
        }
        std::cout << std::endl;

        std::cout << "Real Interface Name: " << netifs[i].ifreal << std::endl;

        std::cout << "---------------------------------" << std::endl;
    }

    return 0;
}

如下为命令操作:

查看虚IP

ip -f inet addr

删除指定虚IP

ip addr del 172.x.x.x dev ens33

新增虚IP:

ifconfig ens33:1 172.x.x.x netmask 255.255.250.0

以上注意需要加:1这些来新增一个设备,添加完后在ifconfig 能看到新加的IP。

如下的命令给加到同一个设备上,导致ifconfig看不到,ip a下看得到:

ip addr add 192.168.0.102/24 dev eth0

相关推荐
hgdlip6 分钟前
手机上网可以固定ip地址吗?详细解析
网络·tcp/ip·智能手机
2501_9159214310 分钟前
高敏感应用如何保护自身不被逆向?iOS 安全加固策略与工具组合实战(含 Ipa Guard 等)
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_9151063235 分钟前
App 上线后还能加固吗?iOS 应用的动态安全补强方案实战分享(含 Ipa Guard 等工具组合)
websocket·网络协议·tcp/ip·http·网络安全·https·udp
MyY_DO36 分钟前
通讯录实现(Linux+Cpp)
linux·运维·服务器
独行soc38 分钟前
2025年渗透测试面试题总结-腾讯[实习]玄武实验室-安全工程师(题目+回答)
linux·安全·web安全·面试·职场和发展·渗透测试·区块链
Nightmare0041 小时前
ubuntu22.04安装taskfile
运维·服务器·taskfile
自动驾驶小卡2 小时前
ubuntu 常用操作指令(与域控制器交互相关)
linux·ubuntu·操作指令
靡樊2 小时前
Socket编程UDP\TCP
网络·c++·学习·tcp/ip·udp
意如流水任东西3 小时前
Linux开发工具(apt,vim,gcc)
linux·服务器