功能:
1、mac地址转换
2、ip地址转换
3、校验ip
4、根据掩码,获取ip的子网起始与终止ip
5、根据掩码,获取子网的所有ip
package com.lms.arp.util;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* @Author: 李孟帅
* @Date: 2021-11-15 18:41
* @Description:
*/
public class NetUtil {
// ip正则匹配
private static final Pattern PATTERN = Pattern.compile("(([\\d]{1,2}|1[\\d]{1,2}|2[0-4]\\d|25[0-5])\\.){3}([\\d]{1,2}|1[\\d]{1,2}|2[0-4]\\d|25[0-5])");
/**
* @Author 李孟帅
* @Date 2021-11-16 16:20
* @Description TODO 6个字节byte数组转为mac地址字符串
*/
public static String byteToMac(byte[] byteMac) {
StringBuilder sb = new StringBuilder();
for (byte b : byteMac) {
String x = Integer.toHexString(b & 0xff).toUpperCase();
sb.append(x.length() == 1 ? "0" + x : x).append("-");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
/**
* @Author 李孟帅
* @Date 2021-11-16 14:17
* @Description TODO 4个字节byte数组转为ip地址字符串
*/
public static String byteToIp(byte[] byteIp) {
StringBuilder sb = new StringBuilder();
for (byte b : byteIp) {
int a = b & 0xff;
sb.append(a).append(".");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
/**
* @Author 李孟帅
* @Date 2021-11-16 9:03
* @Description TODO mac地址字符串转为6byte数组 str : 00-E3-4C-68-0A-64
*/
public static byte[] macToByte(String str) {
byte[] mac = new byte[6];
String[] s1 = str.split("-");
for (int x = 0; x < s1.length; x++) {
mac[x] = (byte) ((Integer.parseInt(s1[x], 16)) & 0xff);
}
return mac;
}
/**
* @Author 李孟帅
* @Date 2021-11-16 15:52
* @Description TODO 将整数转为ip(eg:192.100.3.100)字符串格式
*/
public static String longToIp(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;
}
/**
* @Author 李孟帅
* @Date 2021-11-16 15:53
* @Description TODO 将ip字符串(eg:192.100.3.100)转为整数
*/
public static long ipToLong(String ip) {
long ipLong = 0L;
String ipTemp = ip;
ipLong = ipLong * 256
+ Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
ipTemp = ipTemp.substring(ipTemp.indexOf('.') + 1);
ipLong = ipLong * 256
+ Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1);
ipLong = ipLong * 256
+ Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
ipTemp = ipTemp.substring(ipTemp.indexOf('.') + 1);
ipLong = ipLong * 256 + Long.parseLong(ipTemp);
return ipLong;
}
/**
* @Author 李孟帅
* @Date 2021-11-16 14:45
* @Description TODO 根据掩码 获取ip的起始ip
*/
public static String getBeginIp(String ip, Mask mask) throws MaskParseException {
return getBeginIp(ip, mask.getMask());
}
/**
* @Author 李孟帅
* @Date 2021-11-16 14:45
* @Description TODO 根据掩码 获取ip的起始ip
*/
public static String getBeginIp(String ip, String mask) throws MaskParseException {
checkMask(mask);
return longToIp(ipToLong(ip) & ipToLong(mask));
}
/**
* @Author 李孟帅
* @Date 2021-11-16 16:51
* @Description TODO cidr: 192.100.3.100/24
*/
public static String getBeginIp(String cidr) throws MaskParseException {
String[] split = cidr.split("/");
return getBeginIp(split[0], Mask.parse(Integer.parseInt(split[1])));
}
/**
* @Author 李孟帅
* @Date 2021-11-16 14:45
* @Description TODO 根据掩码 获取ip的终止ip
*/
public static String getEndIp(String ip, Mask mask) throws MaskParseException {
return getEndIp(ip, mask.getMask());
}
/**
* @Author 李孟帅
* @Date 2021-11-16 14:45
* @Description TODO 根据掩码 获取ip的终止ip
*/
public static String getEndIp(String ip, String mask) throws MaskParseException {
checkMask(mask);
long l = ipToLong(mask);
return longToIp((ipToLong(ip) & l) + ~l);
}
/**
* @Author 李孟帅
* @Date 2021-11-16 14:45
* @Description TODO 根据掩码 获取ip的终止ip
*/
public static String getEndIp(String cidr) throws MaskParseException {
String[] split = cidr.split("/");
return getEndIp(split[0], Mask.parse(Integer.parseInt(split[1])));
}
/**
* @Author 李孟帅
* @Date 2021-11-16 15:54
* @Description TODO 获取起始ip和终止ip
*/
public static String getNetRange(String ip, Mask mask) throws MaskParseException {
return getNetRange(ip, mask.getMask());
}
/**
* @Author 李孟帅
* @Date 2021-11-16 15:54
* @Description TODO 获取起始ip和终止ip
*/
public static String getNetRange(String ip, String mask) throws MaskParseException {
checkMask(mask);
String beginIp = getBeginIp(ip, mask);
String endIp = getEndIp(ip, mask);
return beginIp + " ~ " + endIp;
}
/**
* @Author 李孟帅
* @Date 2021-11-16 15:54
* @Description TODO 获取起始ip和终止ip
*/
public static String getNetRange(String cidr) throws MaskParseException {
String[] split = cidr.split("/");
return getNetRange(split[0], Mask.parse(Integer.parseInt(split[1])));
}
/**
* @Author 李孟帅
* @Date 2021-11-16 15:55
* @Description TODO 获取子网下的所有ip地址
*/
public static List<String> getSubnetIps(String ip, Mask mask) throws MaskParseException {
return getSubnetIps(ip, mask.getMask());
}
/**
* @Author 李孟帅
* @Date 2021-11-16 15:55
* @Description TODO 获取子网下的所有ip地址
*/
public static List<String> getSubnetIps(String ip, String mask) throws MaskParseException {
checkMask(mask);
List<String> list = new ArrayList<>();
String beginIp = getBeginIp(ip, mask);
String endIp = getEndIp(ip, mask);
long start = ipToLong(beginIp);
long end = ipToLong(endIp);
for (long i = start; i <= end; i++) {
list.add(longToIp(i));
}
return list;
}
/**
* @Author 李孟帅
* @Date 2021-11-16 15:55
* @Description TODO 获取子网下的所有ip地址
*/
public static List<String> getSubnetIps(String cidr) throws MaskParseException {
String[] split = cidr.split("/");
return getSubnetIps(split[0], Mask.parse(Integer.parseInt(split[1])));
}
/**
* @Author 李孟帅
* @Date 2021-11-16 16:44
* @Description TODO 返回ip个数,setScale(1)//表示保留一位小数,默认四舍五入方式
*/
public static int getIpCount(int maskBit) {
double pow = Math.pow(2, 32 - maskBit);
return BigDecimal.valueOf(pow).setScale(0, BigDecimal.ROUND_DOWN).intValue();
}
/**
* @Author 李孟帅
* @Date 2021-11-16 16:50
* @Description TODO 检查ip是否在某个子网下
*/
public static boolean checkIpInSubnetRange(String ip, String cidr) throws MaskParseException {
List<String> subnetIps = getSubnetIps(cidr);
return subnetIps.contains(ip);
}
/**
* @Author 李孟帅
* @Date 2021-11-16 16:22
* @Description TODO 校验ip地址的有效性
*/
public static boolean checkIp(String ip) {
return PATTERN.matcher(ip).matches();
}
/**
* @Author 李孟帅
* @Date 2021-11-16 17:53
* @Description TODO 校验mask
*/
public static boolean checkMask(String mask) throws MaskParseException {
Mask.parse(mask);
return true;
}
public enum Mask {
BIT_1("128.0.0.0"),
BIT_2("192.0.0.0"),
BIT_3("224.0.0.0"),
BIT_4("240.0.0.0"),
BIT_5("248.0.0.0"),
BIT_6("252.0.0.0"),
BIT_7("254.0.0.0"),
BIT_8("255.0.0.0"),
BIT_9("255.128.0.0"),
BIT_10("255.192.0.0"),
BIT_11("255.224.0.0"),
BIT_12("255.240.0.0"),
BIT_13("255.248.0.0"),
BIT_14("255.252.0.0"),
BIT_15("255.254.0.0"),
BIT_16("255.255.0.0"),
BIT_17("255.255.128.0"),
BIT_18("255.255.192.0"),
BIT_19("255.255.224.0"),
BIT_20("255.255.240.0"),
BIT_21("255.255.248.0"),
BIT_22("255.255.252.0"),
BIT_23("255.255.254.0"),
BIT_24("255.255.255.0"),
BIT_25("255.255.255.128"),
BIT_26("255.255.255.192"),
BIT_27("255.255.255.224"),
BIT_28("255.255.255.240"),
BIT_29("255.255.255.248"),
BIT_30("255.255.255.252"),
BIT_31("255.255.255.254"),
BIT_32("255.255.255.255");
String mask;
Mask(String mask) {
this.mask = mask;
}
public String getMask() {
return mask;
}
public static Mask parse(String mask) throws MaskParseException {
Mask[] values = values();
for (Mask value : values) {
if (value.mask.equals(mask)) {
return value;
}
}
throw new MaskParseException("未知的子网掩码");
}
public static Mask parse(int maskBit) throws MaskParseException {
if (maskBit < 1 || maskBit > 32) {
throw new MaskParseException("无效的子网掩码位数");
}
return values()[maskBit - 1];
}
}
private static class MaskParseException extends Exception {
public MaskParseException(String message) {
super(message);
}
}
}