Java解析网段下包含的所有IP地址
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; public class IpUtils { public static void main(String[] args) { String ip="192.168.100.200"; String mask="31"; System.out.println(parseIpMaskRange(ip, mask).size());; } public static List<String> parseIpMaskRange(String ip,String mask){ List<String> list=new ArrayList<String>(); if ("32".equals(mask)) { list.add(ip); }else{ String startIp=getBeginIpStr(ip, mask); String endIp=getEndIpStr(ip, mask); if (!"31".equals(mask)) { String subStart=startIp.split("\\.")[0]+"."+startIp.split("\\.")[1]+"."+startIp.split("\\.")[2]+"."; String subEnd=endIp.split("\\.")[0]+"."+endIp.split("\\.")[1]+"."+endIp.split("\\.")[2]+"."; startIp=subStart+(Integer.valueOf(startIp.split("\\.")[3])+1); endIp=subEnd+(Integer.valueOf(endIp.split("\\.")[3])-1); } list=parseIpRange(startIp, endIp); } return list; } public static List<String> parseIpRange(String ipfrom, String ipto) { List<String> ips = new ArrayList<String>(); String[] ipfromd = ipfrom.split("\\."); String[] iptod = ipto.split("\\."); int[] int_ipf = new int[4]; int[] int_ipt = new int[4]; for (int i = 0; i < 4; i++) { int_ipf[i] = Integer.parseInt(ipfromd[i]); int_ipt[i] = Integer.parseInt(iptod[i]); } for (int A = int_ipf[0]; A <= int_ipt[0]; A++) { for (int B = (A == int_ipf[0] ? int_ipf[1] : 0); B <= (A == int_ipt[0] ? int_ipt[1] : 255); B++) { for (int C = (B == int_ipf[1] ? int_ipf[2] : 0); C <= (B == int_ipt[1] ? int_ipt[2] : 255); C++) { for (int D = (C == int_ipf[2] ? int_ipf[3] : 0); D <= (C == int_ipt[2] ? int_ipt[3] : 255); D++) { ips.add(new String(A + "." + B + "." + C + "." + D)); } } } } return ips; } /** * 把long类型的Ip转为一般Ip类型:xx.xx.xx.xx * * @param ip * @return */ public static String getIpFromLong(Long ip) { String s1 = String.valueOf((ip & 4278190080L) / 16777216L); String s2 = String.valueOf((ip & 16711680L) / 65536L); String s3 = String.valueOf((ip & 65280L) / 256L); String s4 = String.valueOf(ip & 255L); return s1 + "." + s2 + "." + s3 + "." + s4; } /** * 把xx.xx.xx.xx类型的转为long类型的 * * @param ip * @return */ public static Long getIpFromString(String ip) { Long ipLong = 0L; String ipTemp = ip; ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf("."))); ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length()); ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf("."))); ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length()); ipLong = ipLong * 256 + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf("."))); ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length()); ipLong = ipLong * 256 + Long.parseLong(ipTemp); return ipLong; } /** * 根据掩码位获取掩码 * * @param maskBit * 掩码位数,如"28"、"30" * @return */ public static String getMaskByMaskBit(String maskBit) { return StringUtils.isEmpty(maskBit) ? "error, maskBit is null !" : maskBitMap().get(maskBit); } /** * 根据 ip/掩码位 计算IP段的起始IP 如 IP串 218.240.38.69/30 * * @param ip * 给定的IP,如218.240.38.69 * @param maskBit * 给定的掩码位,如30 * @return 起始IP的字符串表示 */ public static String getBeginIpStr(String ip, String maskBit) { return getIpFromLong(getBeginIpLong(ip, maskBit)); } /** * 根据 ip/掩码位 计算IP段的起始IP 如 IP串 218.240.38.69/30 * * @param ip * 给定的IP,如218.240.38.69 * @param maskBit * 给定的掩码位,如30 * @return 起始IP的长整型表示 */ public static Long getBeginIpLong(String ip, String maskBit) { return getIpFromString(ip) & getIpFromString(getMaskByMaskBit(maskBit)); } /** * 根据 ip/掩码位 计算IP段的终止IP 如 IP串 218.240.38.69/30 * * @param ip * 给定的IP,如218.240.38.69 * @param maskBit * 给定的掩码位,如30 * @return 终止IP的字符串表示 */ public static String getEndIpStr(String ip, String maskBit) { return getIpFromLong(getEndIpLong(ip, maskBit)); } /** * 根据 ip/掩码位 计算IP段的终止IP 如 IP串 218.240.38.69/30 * * @param ip * 给定的IP,如218.240.38.69 * @param maskBit * 给定的掩码位,如30 * @return 终止IP的长整型表示 */ public static Long getEndIpLong(String ip, String maskBit) { return getBeginIpLong(ip, maskBit) + ~getIpFromString(getMaskByMaskBit(maskBit)); } /** * 根据子网掩码转换为掩码位 如 255.255.255.252转换为掩码位 为 30 * * @param netmarks * @return */ public static int getNetMask(String netmarks) { StringBuffer sbf; String str; int inetmask = 0, count = 0; String[] ipList = netmarks.split("\\."); for (int n = 0; n < ipList.length; n++) { sbf = toBin(Integer.parseInt(ipList[n])); str = sbf.reverse().toString(); count = 0; for (int i = 0; i < str.length(); i++) { i = str.indexOf('1', i); if (i == -1) { break; } count++; } inetmask += count; } return inetmask; } /** * 计算子网大小 * * @param netmask * 掩码位 * @return */ public static int getPoolMax(int maskBit) { if (maskBit <= 0 || maskBit >= 32) { return 0; } return (int) Math.pow(2, 32 - maskBit) - 2; } private static StringBuffer toBin(int x) { StringBuffer result = new StringBuffer(); result.append(x % 2); x /= 2; while (x > 0) { result.append(x % 2); x /= 2; } return result; } /* * 存储着所有的掩码位及对应的掩码 key:掩码位 value:掩码(x.x.x.x) */ private static Map<String, String> maskBitMap() { Map<String, String> maskBit = new HashMap<String, String>(); maskBit.put("1", "128.0.0.0"); maskBit.put("2", "192.0.0.0"); maskBit.put("3", "224.0.0.0"); maskBit.put("4", "240.0.0.0"); maskBit.put("5", "248.0.0.0"); maskBit.put("6", "252.0.0.0"); maskBit.put("7", "254.0.0.0"); maskBit.put("8", "255.0.0.0"); maskBit.put("9", "255.128.0.0"); maskBit.put("10", "255.192.0.0"); maskBit.put("11", "255.224.0.0"); maskBit.put("12", "255.240.0.0"); maskBit.put("13", "255.248.0.0"); maskBit.put("14", "255.252.0.0"); maskBit.put("15", "255.254.0.0"); maskBit.put("16", "255.255.0.0"); maskBit.put("17", "255.255.128.0"); maskBit.put("18", "255.255.192.0"); maskBit.put("19", "255.255.224.0"); maskBit.put("20", "255.255.240.0"); maskBit.put("21", "255.255.248.0"); maskBit.put("22", "255.255.252.0"); maskBit.put("23", "255.255.254.0"); maskBit.put("24", "255.255.255.0"); maskBit.put("25", "255.255.255.128"); maskBit.put("26", "255.255.255.192"); maskBit.put("27", "255.255.255.224"); maskBit.put("28", "255.255.255.240"); maskBit.put("29", "255.255.255.248"); maskBit.put("30", "255.255.255.252"); maskBit.put("31", "255.255.255.254"); maskBit.put("32", "255.255.255.255"); return maskBit; } /** * 根据掩码位获取掩码 * * @param masks * @return */ public static String getMaskByMaskBit(int masks) { String ret = ""; if (masks == 1) ret = "128.0.0.0"; else if (masks == 2) ret = "192.0.0.0"; else if (masks == 3) ret = "224.0.0.0"; else if (masks == 4) ret = "240.0.0.0"; else if (masks == 5) ret = "248.0.0.0"; else if (masks == 6) ret = "252.0.0.0"; else if (masks == 7) ret = "254.0.0.0"; else if (masks == 8) ret = "255.0.0.0"; else if (masks == 9) ret = "255.128.0.0"; else if (masks == 10) ret = "255.192.0.0"; else if (masks == 11) ret = "255.224.0.0"; else if (masks == 12) ret = "255.240.0.0"; else if (masks == 13) ret = "255.248.0.0"; else if (masks == 14) ret = "255.252.0.0"; else if (masks == 15) ret = "255.254.0.0"; else if (masks == 16) ret = "255.255.0.0"; else if (masks == 17) ret = "255.255.128.0"; else if (masks == 18) ret = "255.255.192.0"; else if (masks == 19) ret = "255.255.224.0"; else if (masks == 20) ret = "255.255.240.0"; else if (masks == 21) ret = "255.255.248.0"; else if (masks == 22) ret = "255.255.252.0"; else if (masks == 23) ret = "255.255.254.0"; else if (masks == 24) ret = "255.255.255.0"; else if (masks == 25) ret = "255.255.255.128"; else if (masks == 26) ret = "255.255.255.192"; else if (masks == 27) ret = "255.255.255.224"; else if (masks == 28) ret = "255.255.255.240"; else if (masks == 29) ret = "255.255.255.248"; else if (masks == 30) ret = "255.255.255.252"; else if (masks == 31) ret = "255.255.255.254"; else if (masks == 32) ret = "255.255.255.255"; return ret; } } |
Java解析网段下包含的所有IP地址的更多相关文章
- 多路由器环境下路由器的入口IP地址及DHCP设置探讨
多路由器环境下路由器的入口IP地址及DHCP设置探讨 这里把路由器的LAN口管理IP地址称为路由器的入口地址,把直接接入互联网的路由器称为主路由器,其他路由器称为从路由器.在多路由器环境下路由器的设置 ...
- Windows下获取本机IP地址方法介绍
Windows下获取本机IP地址方法介绍 if((hostinfo = gethostbyname(name)) != NULL) { #if 1 ; printf("IP COUNT: % ...
- JAVA获取客户端请求的当前网络ip地址(附:Nginx反向代理后获取客户端请求的真实IP)
1. JAVA获取客户端请求的当前网络ip地址: /** * 获取客户端请求的当前网络ip * @param request * @return */ public static String get ...
- Linux下获取本机IP地址的代码
Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...
- QT5下获取本机IP地址、计算机名、网络连接名、MAC地址、子网掩码、广播地址
获取主机名称 /* * 名称:get_localmachine_name * 功能:获取本机机器名称 * 参数:no * 返回:QString */ QString CafesClient::get_ ...
- 如何在 Linux 下大量屏蔽恶意 IP 地址
很多情况下,你可能需要在Linux下屏蔽IP地址.比如,作为一个终端用户,你可能想要免受间谍软件或者IP追踪的困扰.或者当你在运行P2P软件时.你可能想要过滤反P2P活动的网络链接.如果你是一名系统管 ...
- VMware虚拟机下Centos8 设置静态IP地址
缘起 我们在平时学习Redis.Nginx等分布式微服务的组件的时候,无法避免的需要用到Linux操作系统,而Linux操作系统的主机来源差不多就三种情况: 真实物理机 阿里云等云服务器 利用虚拟机 ...
- VMware Fusion DHCP方式下如何指定虚拟机IP地址
默认情况下,vmware fusion中的虚拟机,网卡设置成dhcp(动态分配 )时,会分配一个IP地址,但这个IP通常很难记,如果我们想为某台虚拟机挑一个好记的IP地址,可以按如下步骤操作: 命令行 ...
- Virtual Box下虚拟机复制后ip地址重复
通过桥接模式上网的虚拟机在复制之后,出现三台机器的ip地址都是一样的,还都可以上网, 主要是因为在复制的时候,把网卡信息啥的都一起复制了, 为了设置为不同的ip,需要修改复制后的机器的mac地址. 首 ...
随机推荐
- MVC 构建图片/文件选择器 参考其它CMS功能
实现结果,如下 点击选择图片,弹出一个iframe框 顶部默认图片根目录,依次下面是文件列表 底部是选择的文件地址,以及上传新的图片和文件 加载iframe 调用js方法 function initF ...
- svchost占用cpu
发现有一个svchost始终占用cpu. 查看任务管理器PID列,在tasklist /svc中查找相关列,对应若干服务. 一个一个检查这些服务,确认是windows update服务引起,停止win ...
- Python 基本语法 学习之路(三)
定义变量 在Python中,定义一个变量是很简单的.而且,在Python中,定义是不需要用分号结尾的.例如: a = 10 b = 3 print(a*b) 判断语句 Pyhon的if判断语句是由if ...
- 常见sizeof 笔试题
最近面试过程中遇到了很多很多sizeof的问题. 现在总结一下: #include <stdio.h> #include <string.h> void fun(int arr ...
- sql通用的存储过程
QUOTED_IDENTIFIER ON GO ALTER proc [dbo].[UpPagerSingle] @ReturnFields Varchar(500)='*',--搜索表的字段,比如: ...
- C++调用C函数
http://blog.csdn.net/imcainiao11/article/details/7369447 前言:以前见到extern "C"这样的语句,只是简单地知道跟外部 ...
- 探 寻 宝 藏--- DP
题目描述 传说HMH大沙漠中有一个M*N迷宫,里面藏有许多宝物.某天,Dr.Kong找到了迷宫的地图,他发现迷宫内处处有宝物,最珍贵的宝物就藏在右下角,迷宫的进出口在左上角.当然,迷宫中的通路不是平坦 ...
- System.Security.SecurityException The source was not found, but some or all event logs could not be searched.Inaccessible logs Security.
An exception occurred during the Install phase. System.Security.SecurityException The source was not ...
- 查询目前运行状态-CPU等情况
对目前的数据库的运行状况有一个基本的了解 ) DB_NAME(a.dbid) AS dbname , loginame , spid , cpu , b.text , lastwaittype , w ...
- sikuli实战记录
最近,为了解放运营人力,需要对某国企的系统进行自动化操作.该系统使用的是https,需要加载证书,而且非得用IE才行,firefox无法正常的加载证书.而selenium启动的IE是纯净的,不会加载任 ...