importjava.math.BigDecimal;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.regex.Pattern;
publicclassIpUtil {
publicstaticvoidmain(String[] args) {
Stringip="172.31.0.4";Stringmask="24";Stringsubmark=getMaskMap(mask);
System.out.println("子网掩码为:"+submark);
StringstartIp=getBeginIpStr(ip, mask);
StringendIp=getEndIpStr(ip, mask);
System.out.println("起始IP:"+startIp+"终止IP:"+endIp);
intipCount=getIpCount("24");
System.out.println("ip个数:"+ipCount);
StringsubStart=startIp.split("\\.")[0]+"."+startIp.split("\\.")[1]+"."+startIp.split("\\.")[2]+".";
StringsubEnd=endIp.split("\\.")[0]+"."+endIp.split("\\.")[1]+"."+endIp.split("\\.")[2]+".";
startIp=subStart+(Integer.parseInt(startIp.split("\\.")[3])+1);
endIp=subEnd+(Integer.parseInt(endIp.split("\\.")[3])-1);
System.out.println("起始IP:"+startIp+"终止IP:"+endIp);
booleanflag=isInRange("10.2.0.0", "10.3.0.0/17");
System.out.println(flag);
System.out.println(isIP("192.168.1.0"));
System.out.println(ipToDouble("192.168.1.1"));
}
publicstaticbooleanisInRange(Stringip, Stringcidr) {
String[] ips=ip.split("\\.");
intipAddr= (Integer.parseInt(ips[0]) <<24)
| (Integer.parseInt(ips[1]) <<16)
| (Integer.parseInt(ips[2]) <<8) |Integer.parseInt(ips[3]);
inttype=Integer.parseInt(cidr.replaceAll(".*/", ""));
intmask=0xFFFFFFFF<< (32-type);
StringcidrIp=cidr.replaceAll("/.*", "");
String[] cidrIps=cidrIp.split("\\.");
intcidrIpAddr= (Integer.parseInt(cidrIps[0]) <<24)
| (Integer.parseInt(cidrIps[1]) <<16)
| (Integer.parseInt(cidrIps[2]) <<8)
|Integer.parseInt(cidrIps[3]);
return (ipAddr&mask) == (cidrIpAddr&mask);
}
publicstaticList<String>parseIpMaskRange(Stringip,Stringmask){
List<String>list=newArrayList<>();
if ("32".equals(mask)) {
list.add(ip);
}else{
StringstartIp=getBeginIpStr(ip, mask);
StringendIp=getEndIpStr(ip, mask);
if (!"31".equals(mask)) {
StringsubStart=startIp.split("\\.")[0]+"."+startIp.split("\\.")[1]+"."+startIp.split("\\.")[2]+".";
StringsubEnd=endIp.split("\\.")[0]+"."+endIp.split("\\.")[1]+"."+endIp.split("\\.")[2]+".";
startIp=subStart+(Integer.parseInt(startIp.split("\\.")[3])+1);
endIp=subEnd+(Integer.parseInt(endIp.split("\\.")[3])-1);
}
list=parseIpRange(startIp, endIp);
}
returnlist;
}
publicstaticintgetIpCount(Stringmask) {
returnBigDecimal.valueOf(Math.pow(2, 32-Integer.parseInt(mask))).setScale(0, BigDecimal.ROUND_DOWN).intValue(); }
publicstaticbooleanisIP(Stringstr) {
Stringregex="\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
Patternpattern=Pattern.compile(regex);
returnpattern.matcher(str).matches();
}
publicstaticList<String>parseIpRange(Stringipfrom, Stringipto) {
List<String>ips=newArrayList<String>();
String[] ipfromd=ipfrom.split("\\.");
String[] iptod=ipto.split("\\.");
int[] int_ipf=newint[4];
int[] int_ipt=newint[4];
for (inti=0; i<4; i++) {
int_ipf[i] =Integer.parseInt(ipfromd[i]);
int_ipt[i] =Integer.parseInt(iptod[i]);
}
for (intA=int_ipf[0]; A<=int_ipt[0]; A++) {
for (intB= (A==int_ipf[0] ?int_ipf[1] : 0); B<= (A==int_ipt[0] ?int_ipt[1]
: 255); B++) {
for (intC= (B==int_ipf[1] ?int_ipf[2] : 0); C<= (B==int_ipt[1] ?int_ipt[2]
: 255); C++) {
for (intD= (C==int_ipf[2] ?int_ipf[3] : 0); D<= (C==int_ipt[2] ?int_ipt[3]
: 255); D++) {
ips.add(A+"."+B+"."+C+"."+D);
}
}
}
}
returnips;
}
publicstaticStringgetIpFromLong(Longip)
{
Strings1=String.valueOf((ip&4278190080L) /16777216L);
Strings2=String.valueOf((ip&16711680L) /65536L);
Strings3=String.valueOf((ip&65280L) /256L);
Strings4=String.valueOf(ip&255L);
returns1+"."+s2+"."+s3+"."+s4;
}
publicstaticLonggetIpFromString(Stringip)
{
LongipLong=0L;
StringipTemp=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);
returnipLong;
}
publicstaticStringgetMaskByMaskBit(StringmaskBit)
{
return"".equals(maskBit) ?"error, maskBit is null !" : getMaskMap(maskBit);
}
publicstaticStringgetBeginIpStr(Stringip, StringmaskBit)
{
returngetIpFromLong(getBeginIpLong(ip, maskBit));
}
publicstaticLonggetBeginIpLong(Stringip, StringmaskBit)
{
returngetIpFromString(ip) &getIpFromString(getMaskByMaskBit(maskBit));
}
publicstaticStringgetEndIpStr(Stringip, StringmaskBit)
{
returngetIpFromLong(getEndIpLong(ip, maskBit));
}
publicstaticLonggetEndIpLong(Stringip, StringmaskBit)
{
returngetBeginIpLong(ip, maskBit)
+~getIpFromString(getMaskByMaskBit(maskBit));
}
publicstaticintgetNetMask(Stringnetmarks)
{
StringBuildersbf;
Stringstr;
intinetmask=0;
intcount=0;
String[] ipList=netmarks.split("\\.");
for (intn=0; n<ipList.length; n++)
{
sbf=toBin(Integer.parseInt(ipList[n]));
str=sbf.reverse().toString();
count=0;
for (inti=0; i<str.length(); i++)
{
i=str.indexOf('1', i);
if (i==-1)
{
break;
}
count++;
}
inetmask+=count;
}
returninetmask;
}
publicstaticintgetPoolMax(intmaskBit)
{
if (maskBit<=0||maskBit>=32)
{
return0;
}
return (int) Math.pow(2, 32-maskBit) -2;
}
privatestaticStringBuildertoBin(intx)
{
StringBuilderresult=newStringBuilder();
result.append(x%2);
x/=2;
while (x>0)
{
result.append(x%2);
x/=2;
}
returnresult;
}
publicstaticStringgetMaskMap(StringmaskBit) {
if ("1".equals(maskBit)) {
return"128.0.0.0";
}
if ("2".equals(maskBit)) {
return"192.0.0.0";
}
if ("3".equals(maskBit)) {
return"224.0.0.0";
}
if ("4".equals(maskBit)) {
return"240.0.0.0";
}
if ("5".equals(maskBit)) {
return"248.0.0.0";
}
if ("6".equals(maskBit)) {
return"252.0.0.0";
}
if ("7".equals(maskBit)) {
return"254.0.0.0";
}
if ("8".equals(maskBit)) {return"255.0.0.0";}
if ("9".equals(maskBit)) {return"255.128.0.0";}
if ("10".equals(maskBit)) {return"255.192.0.0";}
if ("11".equals(maskBit)) {return"255.224.0.0";}
if ("12".equals(maskBit)) {return"255.240.0.0";}
if ("13".equals(maskBit)) {return"255.248.0.0";}
if ("14".equals(maskBit)) {return"255.252.0.0";}
if ("15".equals(maskBit)) {return"255.254.0.0";}
if ("16".equals(maskBit)) {return"255.255.0.0";}
if ("17".equals(maskBit)) {return"255.255.128.0";}
if ("18".equals(maskBit)) {return"255.255.192.0";}
if ("19".equals(maskBit)) {return"255.255.224.0";}
if ("20".equals(maskBit)) {return"255.255.240.0";}
if ("21".equals(maskBit)) {return"255.255.248.0";}
if ("22".equals(maskBit)) {return"255.255.252.0";}
if ("23".equals(maskBit)) {return"255.255.254.0";}
if ("24".equals(maskBit)) {return"255.255.255.0";}
if ("25".equals(maskBit)) {return"255.255.255.128";}
if ("26".equals(maskBit)) {return"255.255.255.192";}
if ("27".equals(maskBit)) {return"255.255.255.224";}
if ("28".equals(maskBit)) {return"255.255.255.240";}
if ("29".equals(maskBit)) {return"255.255.255.248";}
if ("30".equals(maskBit)) {return"255.255.255.252";}
if ("31".equals(maskBit)) {return"255.255.255.254";}
if ("32".equals(maskBit)) {return"255.255.255.255";}
return"-1";
}
publicstaticdoubleipToDouble(Stringip) {
String[] arr=ip.split("\\.");
doubled1=Double.parseDouble(arr[0]);
doubled2=Double.parseDouble(arr[1]);
doubled3=Double.parseDouble(arr[2]);
doubled4=Double.parseDouble(arr[3]);
returnd1*Math.pow(256, 3) +d2*Math.pow(256, 2) +d3*256+d4;
}
}