写一个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

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

相关推荐
白帽黑客沐瑶20 小时前
【网络安全就业】信息安全专业的就业前景(非常详细)零基础入门到精通,收藏这篇就够了
网络·安全·web安全·计算机·程序员·编程·网络安全就业
树码小子21 小时前
Java网络编程:(socket API编程:TCP协议的 socket API -- 回显程序的服务器端程序的编写)
java·网络·tcp/ip
绿箭柠檬茶1 天前
Ubuntu 服务器配置转发网络访问
服务器·网络·ubuntu
FPGA_Linuxer1 天前
FPGA 40 DAC线缆和光模块带光纤实现40G UDP差异
网络协议·fpga开发·udp
real 11 天前
传输层协议UDP
网络·网络协议·udp
路由侠内网穿透1 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
喵手1 天前
玩转Java网络编程:基于Socket的服务器和客户端开发!
java·服务器·网络
徐子元竟然被占了!!1 天前
实验-基本ACL
网络
ftpeak1 天前
从零开始使用 axum-server 构建 HTTP/HTTPS 服务
网络·http·https·rust·web·web app
LabVIEW开发1 天前
LabVIEW气体污染无线监测
网络·labview·labview知识·labview功能·labview程序