@Test public void checkIPRangeTest() { assertNull(checkIPRange("", "")); assertNull(checkIPRange("", "10.1.1.0/24")); assertNull(checkIPRange("10.1.1.1", "10.1.1.0/")); assertNull(checkIPRange("10.1.1.1", "/24")); assertNull(checkIPRange("9999.1.1.1", "10.1.1.0/24")); assertEquals(true, checkIPRange("10.1.1.1", "10.1.1.0/24")); assertEquals(false, checkIPRange("10.1.2.1", "10.1.1.0/24")); assertEquals(false, checkIPRange("113.12.83.3", "113.12.83.4/32")); assertEquals(true, checkIPRange("113.12.83.4", "113.12.83.4/32")); String aim = "113.12.83.4"; String ranges = "113.12.83.4/32,113.12.83.5/32,116.1.239.35/32"; boolean result = false; if (ranges.contains(",")) { for (String range : ranges.split(",")) { Boolean isInRange = checkIPRange(aim, range); if (isInRange != null && isInRange) { result = true; break; } } } assertTrue(result); }
public static Boolean checkIPRange(final String ipAddress, final String range) { if (isEmpty(range) || isEmpty(ipAddress) || !range.contains("/")) return null; String[] rangeArray = range.split("/"); if (rangeArray.length != 2) return null; if (isEmpty(rangeArray[0]) || isEmpty(rangeArray[1])) return null; String rangeIp = rangeArray[0]; if (!checkValid(rangeIp) || !checkValid(ipAddress)) return null; Integer subnet = ip2int(rangeIp); if (subnet == null) return false; Integer ip = ip2int(ipAddress); if (ip == null) return false; int mask = -1 << (32 - Integer.parseInt(rangeArray[1])); return (subnet & mask) == (ip & mask); }
IpUtils { public static Boolean checkIPRange(final String ipAddress, final String range) { if (isEmpty(range) || isEmpty(ipAddress) || !range.contains("/")) return null; String[] rangeArray = range.split("/"); if (rangeArray.length != 2) return null; if (isEmpty(rangeArray[0]) || isEmpty(rangeArray[1])) return null; String rangeIp = rangeArray[0]; if (!checkValid(rangeIp) || !checkValid(ipAddress)) return null; Integer subnet = ip2int(rangeIp); if (subnet == null) return false; Integer ip = ip2int(ipAddress); if (ip == null) return false; int mask = -1 << (32 - Integer.parseInt(rangeArray[1])); return (subnet & mask) == (ip & mask); } }
IpUtils { public static Boolean checkIPRange(final String ipAddress, final String range) { if (isEmpty(range) || isEmpty(ipAddress) || !range.contains("/")) return null; String[] rangeArray = range.split("/"); if (rangeArray.length != 2) return null; if (isEmpty(rangeArray[0]) || isEmpty(rangeArray[1])) return null; String rangeIp = rangeArray[0]; if (!checkValid(rangeIp) || !checkValid(ipAddress)) return null; Integer subnet = ip2int(rangeIp); if (subnet == null) return false; Integer ip = ip2int(ipAddress); if (ip == null) return false; int mask = -1 << (32 - Integer.parseInt(rangeArray[1])); return (subnet & mask) == (ip & mask); } private  IpUtils(); }
IpUtils { public static Boolean checkIPRange(final String ipAddress, final String range) { if (isEmpty(range) || isEmpty(ipAddress) || !range.contains("/")) return null; String[] rangeArray = range.split("/"); if (rangeArray.length != 2) return null; if (isEmpty(rangeArray[0]) || isEmpty(rangeArray[1])) return null; String rangeIp = rangeArray[0]; if (!checkValid(rangeIp) || !checkValid(ipAddress)) return null; Integer subnet = ip2int(rangeIp); if (subnet == null) return false; Integer ip = ip2int(ipAddress); if (ip == null) return false; int mask = -1 << (32 - Integer.parseInt(rangeArray[1])); return (subnet & mask) == (ip & mask); } private  IpUtils(); static boolean checkValid(final String ip); static boolean checkValid2(final String ip); static boolean checkValidV6(final String ip); static String removeTail32(String ip); static String extractDomain(String url); static String getTailFromURL(String url); static long ip2long(String ipAddress); static String long2ip(Long ipAddress); static Integer ip2int(String ipAddress); static Boolean checkIPRange(final String ipAddress, final String range); static String getIPFromURL(String url); static Boolean isReachable(final String ipAddress); static boolean ping(final String ipAddress); static List<String> getCurrentEnvironmentNetworkIps(); }
IpUtils { public static Boolean checkIPRange(final String ipAddress, final String range) { if (isEmpty(range) || isEmpty(ipAddress) || !range.contains("/")) return null; String[] rangeArray = range.split("/"); if (rangeArray.length != 2) return null; if (isEmpty(rangeArray[0]) || isEmpty(rangeArray[1])) return null; String rangeIp = rangeArray[0]; if (!checkValid(rangeIp) || !checkValid(ipAddress)) return null; Integer subnet = ip2int(rangeIp); if (subnet == null) return false; Integer ip = ip2int(ipAddress); if (ip == null) return false; int mask = -1 << (32 - Integer.parseInt(rangeArray[1])); return (subnet & mask) == (ip & mask); } private  IpUtils(); static boolean checkValid(final String ip); static boolean checkValid2(final String ip); static boolean checkValidV6(final String ip); static String removeTail32(String ip); static String extractDomain(String url); static String getTailFromURL(String url); static long ip2long(String ipAddress); static String long2ip(Long ipAddress); static Integer ip2int(String ipAddress); static Boolean checkIPRange(final String ipAddress, final String range); static String getIPFromURL(String url); static Boolean isReachable(final String ipAddress); static boolean ping(final String ipAddress); static List<String> getCurrentEnvironmentNetworkIps();  }