功能:

1、mac地址转换

2、ip地址转换

3、校验ip

4、根据掩码,获取ip的子网起始与终止ip

5、根据掩码,获取子网的所有ip

  1. package com.lms.arp.util;
  2. import java.math.BigDecimal;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.regex.Pattern;
  6. /**
  7. * @Author: 李孟帅
  8. * @Date: 2021-11-15 18:41
  9. * @Description:
  10. */
  11. public class NetUtil {
  12. // ip正则匹配
  13. 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])");
  14. /**
  15. * @Author 李孟帅
  16. * @Date 2021-11-16 16:20
  17. * @Description TODO 6个字节byte数组转为mac地址字符串
  18. */
  19. public static String byteToMac(byte[] byteMac) {
  20. StringBuilder sb = new StringBuilder();
  21. for (byte b : byteMac) {
  22. String x = Integer.toHexString(b & 0xff).toUpperCase();
  23. sb.append(x.length() == 1 ? "0" + x : x).append("-");
  24. }
  25. sb.deleteCharAt(sb.length() - 1);
  26. return sb.toString();
  27. }
  28. /**
  29. * @Author 李孟帅
  30. * @Date 2021-11-16 14:17
  31. * @Description TODO 4个字节byte数组转为ip地址字符串
  32. */
  33. public static String byteToIp(byte[] byteIp) {
  34. StringBuilder sb = new StringBuilder();
  35. for (byte b : byteIp) {
  36. int a = b & 0xff;
  37. sb.append(a).append(".");
  38. }
  39. sb.deleteCharAt(sb.length() - 1);
  40. return sb.toString();
  41. }
  42. /**
  43. * @Author 李孟帅
  44. * @Date 2021-11-16 9:03
  45. * @Description TODO mac地址字符串转为6byte数组 str : 00-E3-4C-68-0A-64
  46. */
  47. public static byte[] macToByte(String str) {
  48. byte[] mac = new byte[6];
  49. String[] s1 = str.split("-");
  50. for (int x = 0; x < s1.length; x++) {
  51. mac[x] = (byte) ((Integer.parseInt(s1[x], 16)) & 0xff);
  52. }
  53. return mac;
  54. }
  55. /**
  56. * @Author 李孟帅
  57. * @Date 2021-11-16 15:52
  58. * @Description TODO 将整数转为ip(eg:192.100.3.100)字符串格式
  59. */
  60. public static String longToIp(long ip) {
  61. String s1 = String.valueOf((ip & 4278190080L) / 16777216L);
  62. String s2 = String.valueOf((ip & 16711680L) / 65536L);
  63. String s3 = String.valueOf((ip & 65280L) / 256L);
  64. String s4 = String.valueOf(ip & 255L);
  65. return s1 + "." + s2 + "." + s3 + "." + s4;
  66. }
  67. /**
  68. * @Author 李孟帅
  69. * @Date 2021-11-16 15:53
  70. * @Description TODO 将ip字符串(eg:192.100.3.100)转为整数
  71. */
  72. public static long ipToLong(String ip) {
  73. long ipLong = 0L;
  74. String ipTemp = ip;
  75. ipLong = ipLong * 256
  76. + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
  77. ipTemp = ipTemp.substring(ipTemp.indexOf('.') + 1);
  78. ipLong = ipLong * 256
  79. + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
  80. ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1);
  81. ipLong = ipLong * 256
  82. + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf('.')));
  83. ipTemp = ipTemp.substring(ipTemp.indexOf('.') + 1);
  84. ipLong = ipLong * 256 + Long.parseLong(ipTemp);
  85. return ipLong;
  86. }
  87. /**
  88. * @Author 李孟帅
  89. * @Date 2021-11-16 14:45
  90. * @Description TODO 根据掩码 获取ip的起始ip
  91. */
  92. public static String getBeginIp(String ip, Mask mask) throws MaskParseException {
  93. return getBeginIp(ip, mask.getMask());
  94. }
  95. /**
  96. * @Author 李孟帅
  97. * @Date 2021-11-16 14:45
  98. * @Description TODO 根据掩码 获取ip的起始ip
  99. */
  100. public static String getBeginIp(String ip, String mask) throws MaskParseException {
  101. checkMask(mask);
  102. return longToIp(ipToLong(ip) & ipToLong(mask));
  103. }
  104. /**
  105. * @Author 李孟帅
  106. * @Date 2021-11-16 16:51
  107. * @Description TODO cidr: 192.100.3.100/24
  108. */
  109. public static String getBeginIp(String cidr) throws MaskParseException {
  110. String[] split = cidr.split("/");
  111. return getBeginIp(split[0], Mask.parse(Integer.parseInt(split[1])));
  112. }
  113. /**
  114. * @Author 李孟帅
  115. * @Date 2021-11-16 14:45
  116. * @Description TODO 根据掩码 获取ip的终止ip
  117. */
  118. public static String getEndIp(String ip, Mask mask) throws MaskParseException {
  119. return getEndIp(ip, mask.getMask());
  120. }
  121. /**
  122. * @Author 李孟帅
  123. * @Date 2021-11-16 14:45
  124. * @Description TODO 根据掩码 获取ip的终止ip
  125. */
  126. public static String getEndIp(String ip, String mask) throws MaskParseException {
  127. checkMask(mask);
  128. long l = ipToLong(mask);
  129. return longToIp((ipToLong(ip) & l) + ~l);
  130. }
  131. /**
  132. * @Author 李孟帅
  133. * @Date 2021-11-16 14:45
  134. * @Description TODO 根据掩码 获取ip的终止ip
  135. */
  136. public static String getEndIp(String cidr) throws MaskParseException {
  137. String[] split = cidr.split("/");
  138. return getEndIp(split[0], Mask.parse(Integer.parseInt(split[1])));
  139. }
  140. /**
  141. * @Author 李孟帅
  142. * @Date 2021-11-16 15:54
  143. * @Description TODO 获取起始ip和终止ip
  144. */
  145. public static String getNetRange(String ip, Mask mask) throws MaskParseException {
  146. return getNetRange(ip, mask.getMask());
  147. }
  148. /**
  149. * @Author 李孟帅
  150. * @Date 2021-11-16 15:54
  151. * @Description TODO 获取起始ip和终止ip
  152. */
  153. public static String getNetRange(String ip, String mask) throws MaskParseException {
  154. checkMask(mask);
  155. String beginIp = getBeginIp(ip, mask);
  156. String endIp = getEndIp(ip, mask);
  157. return beginIp + " ~ " + endIp;
  158. }
  159. /**
  160. * @Author 李孟帅
  161. * @Date 2021-11-16 15:54
  162. * @Description TODO 获取起始ip和终止ip
  163. */
  164. public static String getNetRange(String cidr) throws MaskParseException {
  165. String[] split = cidr.split("/");
  166. return getNetRange(split[0], Mask.parse(Integer.parseInt(split[1])));
  167. }
  168. /**
  169. * @Author 李孟帅
  170. * @Date 2021-11-16 15:55
  171. * @Description TODO 获取子网下的所有ip地址
  172. */
  173. public static List<String> getSubnetIps(String ip, Mask mask) throws MaskParseException {
  174. return getSubnetIps(ip, mask.getMask());
  175. }
  176. /**
  177. * @Author 李孟帅
  178. * @Date 2021-11-16 15:55
  179. * @Description TODO 获取子网下的所有ip地址
  180. */
  181. public static List<String> getSubnetIps(String ip, String mask) throws MaskParseException {
  182. checkMask(mask);
  183. List<String> list = new ArrayList<>();
  184. String beginIp = getBeginIp(ip, mask);
  185. String endIp = getEndIp(ip, mask);
  186. long start = ipToLong(beginIp);
  187. long end = ipToLong(endIp);
  188. for (long i = start; i <= end; i++) {
  189. list.add(longToIp(i));
  190. }
  191. return list;
  192. }
  193. /**
  194. * @Author 李孟帅
  195. * @Date 2021-11-16 15:55
  196. * @Description TODO 获取子网下的所有ip地址
  197. */
  198. public static List<String> getSubnetIps(String cidr) throws MaskParseException {
  199. String[] split = cidr.split("/");
  200. return getSubnetIps(split[0], Mask.parse(Integer.parseInt(split[1])));
  201. }
  202. /**
  203. * @Author 李孟帅
  204. * @Date 2021-11-16 16:44
  205. * @Description TODO 返回ip个数,setScale(1)//表示保留一位小数,默认四舍五入方式
  206. */
  207. public static int getIpCount(int maskBit) {
  208. double pow = Math.pow(2, 32 - maskBit);
  209. return BigDecimal.valueOf(pow).setScale(0, BigDecimal.ROUND_DOWN).intValue();
  210. }
  211. /**
  212. * @Author 李孟帅
  213. * @Date 2021-11-16 16:50
  214. * @Description TODO 检查ip是否在某个子网下
  215. */
  216. public static boolean checkIpInSubnetRange(String ip, String cidr) throws MaskParseException {
  217. List<String> subnetIps = getSubnetIps(cidr);
  218. return subnetIps.contains(ip);
  219. }
  220. /**
  221. * @Author 李孟帅
  222. * @Date 2021-11-16 16:22
  223. * @Description TODO 校验ip地址的有效性
  224. */
  225. public static boolean checkIp(String ip) {
  226. return PATTERN.matcher(ip).matches();
  227. }
  228. /**
  229. * @Author 李孟帅
  230. * @Date 2021-11-16 17:53
  231. * @Description TODO 校验mask
  232. */
  233. public static boolean checkMask(String mask) throws MaskParseException {
  234. Mask.parse(mask);
  235. return true;
  236. }
  237. public enum Mask {
  238. BIT_1("128.0.0.0"),
  239. BIT_2("192.0.0.0"),
  240. BIT_3("224.0.0.0"),
  241. BIT_4("240.0.0.0"),
  242. BIT_5("248.0.0.0"),
  243. BIT_6("252.0.0.0"),
  244. BIT_7("254.0.0.0"),
  245. BIT_8("255.0.0.0"),
  246. BIT_9("255.128.0.0"),
  247. BIT_10("255.192.0.0"),
  248. BIT_11("255.224.0.0"),
  249. BIT_12("255.240.0.0"),
  250. BIT_13("255.248.0.0"),
  251. BIT_14("255.252.0.0"),
  252. BIT_15("255.254.0.0"),
  253. BIT_16("255.255.0.0"),
  254. BIT_17("255.255.128.0"),
  255. BIT_18("255.255.192.0"),
  256. BIT_19("255.255.224.0"),
  257. BIT_20("255.255.240.0"),
  258. BIT_21("255.255.248.0"),
  259. BIT_22("255.255.252.0"),
  260. BIT_23("255.255.254.0"),
  261. BIT_24("255.255.255.0"),
  262. BIT_25("255.255.255.128"),
  263. BIT_26("255.255.255.192"),
  264. BIT_27("255.255.255.224"),
  265. BIT_28("255.255.255.240"),
  266. BIT_29("255.255.255.248"),
  267. BIT_30("255.255.255.252"),
  268. BIT_31("255.255.255.254"),
  269. BIT_32("255.255.255.255");
  270. String mask;
  271. Mask(String mask) {
  272. this.mask = mask;
  273. }
  274. public String getMask() {
  275. return mask;
  276. }
  277. public static Mask parse(String mask) throws MaskParseException {
  278. Mask[] values = values();
  279. for (Mask value : values) {
  280. if (value.mask.equals(mask)) {
  281. return value;
  282. }
  283. }
  284. throw new MaskParseException("未知的子网掩码");
  285. }
  286. public static Mask parse(int maskBit) throws MaskParseException {
  287. if (maskBit < 1 || maskBit > 32) {
  288. throw new MaskParseException("无效的子网掩码位数");
  289. }
  290. return values()[maskBit - 1];
  291. }
  292. }
  293. private static class MaskParseException extends Exception {
  294. public MaskParseException(String message) {
  295. super(message);
  296. }
  297. }
  298. }