首页 > 其他分享 >判断网段下是否包含某个IP

判断网段下是否包含某个IP

时间:2023-05-23 10:45:35浏览次数:32  
标签:子网 网段 String IP xxx segs 某个

前两天遇到一个业务问题,需要从不同的表中找出一批IP和一批网段的对应关系,IP格式为:xxx.xxx.xxx.xxx,网段格式为:xxx.xxx.xxx.xxx/xx。

直接上代码,判断方法如下:

     * 判断该网段下是否包含该IP,子网位数包含在网段中
//     * @param ip
//     * @param netAndMask
     * @return
     */
    public static boolean isIpInSegment(String ip, String segmentStr) {
        try {
            String[] segs = segmentStr.split("/");
            String ipSeg = segs[0];
            String maskSeg = segs[1];
            int ipInt = ipToInt(ip);
            int ipSegInt = ipToInt(ipSeg);
            int maskInt = ((1 << Integer.parseInt(maskSeg)) - 1) << (32 - Integer.parseInt(maskSeg));
            return (ipInt & maskInt) == (ipSegInt & maskInt);
        } catch (Exception e) {
            return false;
        }
    }

    private static int ipToInt(String ip) {
        String[] strs = ip.split("\\.");
        int ipInt = 0;
        for (int i = 0; i < 4; i++) {
            ipInt = ipInt << 8 | Integer.parseInt(strs[i]);
        }
        return ipInt;
    }

  写个main方法测试下,如下:

public static void main(String[] args) {
        String ipAddress = "111.0.17.52";
        String addressRange = "111.0.17.0/24";
        if(isIpInSegment(ipAddress,addressRange)){
            System.out.println("在子网中" );
        }else{
            System.out.println("不在子网中" );
        }

    }

  

标签:子网,网段,String,IP,xxx,segs,某个
From: https://www.cnblogs.com/ya-tao/p/17422457.html

相关文章

  • [HMV] Ripper
    0x00配置攻击机IP:172.16.1.25靶机IP:172.16.1.900x01攻击使用Nmap扫描目标靶机开放的端口┌──(root㉿Kali-VM)-[~]└─#nmap-sC-sV-p-172.16.1.90StartingNmap7.93(https://nmap.org)Nmapscanreportfor172.16.1.90Hostisup(0.00055slatenc......
  • zip压缩保留软链接命令
    压缩保留软链接命令-j:将要压缩的内容前面的路径不保留,即下面示例libxxx.so*前面的路径解压缩后不存在,没有该参数则存在-r:递归压缩-y:保留软链接关系zip-jrylibxxx.zip/home/plat/linux_5_10_latest/libxxx.so*上面命令不使用-j参数测试如下:zip-rylibxxx.zip/home/......
  • NOIP2016普及组试题题解
    1.买铅笔代码:#include<bits/stdc++.h>#definelllonglongusingnamespacestd;intn,ans=1e9,a,b;intmain(){ cin>>n; for(inti=1;i<=3;i++){ cin>>a>>b; ans=min(ans,int(ceil(n*1.0/a)*b)); } cout<<ans; return0;}......
  • JavaScript函数
    1函数定义使用function关键字来定义,即functionfName(para,...){statment;...;},可使用在函数声明语句与函数定义表达式这两种形式中函数名称标识符fName。是函数声明语句必需的部分。它的用途就像变量的名字,新定义的函数对象会赋值给这个变量但对函数定义表达式来说......
  • Rip
    第一章:拓扑图:推荐步骤:➢路由器配置➢配置➢配置实验步骤:路由器配置接口配置查看R2接口配置IP地址查看R3接口配置IP地址查看R4接口配置IP地址查看配置启动查看启动查看启动查看启动查看上运行的查看路由表测试三、配置启动查看启动查看启动查看启动查看上运行的查看路由......
  • Unzipping Files In iOS Using ZipArchive
    Inthistutorial,IamgoingtodemonstratehowyoucanzipandunzipfilesfromwithinyouriOSapplications.WewillbeusingathirdpartylibrarycalledZipArchivetoachievethis.Whilethereareacouplesolutionsouttheretozipandunzipfiles,......
  • python pip超时解决方案
    目录pythonpip超时解决方案使用-i选项指定pip源使用--default-timeout选项增加超时时间增加重试次数pythonpip超时解决方案当执行pip3install命令时出现超时错误,通常是由于网络连接不稳定或网络速度较慢引起的。为了解决这个问题,你可以尝试以下方法:使用-i选项指定......
  • post-GWAS: transcriptome-wide association studies (TWAS) 结果解读
    Thetoppanelshowsallofthegenesinthelocus.ThemarginallyTWASassociatedgenesarehighlightedinblue,andthosethatarejointlysignificant(inthiscase,FAM109B)highlightedingreen.Thestatisticsforthejointlysignificantgenesarerepo......
  • pip常用命令
    学python的都知道安装第三方依赖包经常用到pip命令,然后国内pip下载经常比较慢或者根本下载不到,其实可以通过指定国内pip源下载,快多了。通过如下命令安装requests模块:pipinstallrequests 更换PIP源PIP源在国外,速度慢,可以更换为国内源,以下是国内一些常用的PIP源。豆瓣(do......
  • [NOIP2002]级数求和
    题目链接https://ac.nowcoder.com/acm/contest/19305/1044解题思路模拟级数求和AC代码usingnamespacestd;//n:首项doublesn=0,n=1,k;//找到最小的Sn使得Sn>kintmain(){cin>>k;while(sn<=k)sn+=1/n,n++;cout<<n-1;......