写一个code,来检测本机lP和其他的设备有IP冲突,如有则获取一个新的ip

cpp 复制代码
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <string.h>
#include <vector>
#include <unistd.h>
bool isIpConflict(const std::string& localIp, const std::vector<std::string>& ipList) {
    for (const std::string& ip : ipList) {
        if (ip != localIp) {
            return true;
        }
    }
    return false;
}

bool isIpUsed(const std::string& ip) {
    bool is_used = false;
    struct sockaddr_in sa;
    size_t len = sizeof(sa);

    if (inet_pton(AF_INET, ip.c_str(), &sa) == 0) {
        is_used = true;
    } else {
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        if (fd != -1) {
            if (connect(fd, (struct sockaddr*)&sa, len) == 0) {
                is_used = true;
            }
            close(fd);
        }
    }

    return is_used;
}

// 从start到end之间的每个IP地址依次判断是否被使用,直到找到第一个未被使用的IP地址并输出
const std::string getNewIp(const std::string& ip) {
    const int start = 10;   // 起始IP地址(十进制)
    const int end = 254;     // 结束IP地址(十进制)
    const std::string subnet = ip.substr(0, ip.find_last_of('.')) + '.';  // IP地址前缀
    for (int i = start; i <= end; ++i) {
        std::string newIp = subnet + std::to_string(i);
        if (!isIpUsed(newIp)) {
            return newIp;
        }
    }
    return "";
}

int main() {
    // 本地的IP地址:192.168.159.137
    struct ifaddrs *ifaddr;
    std::vector<std::string> ipList{"192.168.159.136","192.168.159.137"}; // 模拟所获得的其他设备IP列表
    // 调用 getifaddrs() 函数获取所有网络接口信息
    if (getifaddrs(&ifaddr) == -1) {
        perror("Error getting network interfaces");
        return 1;
    }
    
    // 遍历每个网络接口
    for (struct ifaddrs* it = ifaddr; it != nullptr; it = it->ifa_next) {
        
        // 只处理 IPv4 类型的接口
        if (it->ifa_addr && it->ifa_addr->sa_family == AF_INET) {
            char ip[INET_ADDRSTRLEN];
            
            // 将 IPv4 地址转换为字符串格式并输出
            inet_ntop(AF_INET, &((sockaddr_in*)it->ifa_addr)->sin_addr, ip, INET_ADDRSTRLEN);
            if(strcmp(it->ifa_name,"ens33")==0) {
                std::cout << "Interface: " << it->ifa_name << ", IP Address: " << ip << std::endl;
                if(isIpConflict(ip,ipList)) {
                    std::cout<<"检测出本机的IP地址和其他的设备有ip地址起冲突了"<<std::endl;
                    std::cout << "获得未被使用的新IP为:"<< getNewIp(ip) << std::endl;
                }
            }
                
        }
    }
    freeifaddrs(ifaddr);
    return 0;
}
cpp 复制代码
Interface: ens33, IP Address: 192.168.159.137
检测出本机的IP地址和其他的设备有ip地址起冲突了
获得未被使用的新IP为:192.168.159.10

此题为小编自己思考做出来的,没有正确答案,仅供参考,欢迎一起学习和交流!

相关推荐
leoFY1231 小时前
STM32H750配置LAN PHY芯片LAN8742
网络·stm32·嵌入式硬件
阿部多瑞 ABU2 小时前
AI红队攻防演化史(2023-2026):从虚拟角色到RLHF劫持——所有攻击方法全景总结与最新趋势分析
网络·人工智能·安全
博客-小覃2 小时前
Zabbix之华为交换机的日志记录信息操作详细教程
服务器·网络·华为·zabbix
stolentime2 小时前
FreeDomain 本地开发环境快速搭建指南
运维·服务器·网络
ytdbc4 小时前
OSPF综合实验
网络
kaisun644 小时前
Docker 构建网络问题排查
网络·docker·eureka
雪度娃娃5 小时前
存储器层次结构——磁盘硬盘存储
服务器·网络·数据库·计算机组成原理
YUANQIANG20245 小时前
通信领域进行蒙特卡洛仿真的思路和步骤
网络
eam0511235 小时前
OSPF综合实验
网络
QQ15401828565 小时前
USB转千兆以太网芯片方案
网络·pt153s·千兆以太网芯片·usb转以太网·千兆网口芯片