该程序,是将当前计算机的 操作系统名前缀 、 主板序列号、 MAC 地址、 CPU 序列号 组合成为JSON 字符串 作为当前计算机的唯一标识串。
import com.stant.CommonConstant;
import lombok.Data;
slf4j.Slf4j;import java.io.*;
import java.InetAddress;
import java.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import Matcher;
import Pattern;/*** <p>* ComputerUniqueIdentificationUtil<br>* 计算机唯一识别实用程序* </p>** @author XinLau* @version 1.0* @since 2020年10月10日 17:14*/
@Slf4j
public class ComputerUniqueIdentificationUtil {/*** Windows*/public static final String WINDOWS = "windows";/*** Linux*/public static final String LINUX = "linux";/*** Unix*/public static final String UNIX = "unix";/*** 正则表达式*/public static final String REGEX = "\b\w+:\w+:\w+:\w+:\w+:\w+\b";/*** 获取 Windows 主板序列号** @return String - 计算机主板序列号* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/private static String getWindowsMainBoardSerialNumber() {String result = "";try {File file = ateTempFile("realhowto", ".vbs");file.deleteOnExit();FileWriter fw = new java.io.FileWriter(file);String vbs = "Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")n"+ "Set colItems = objWMIService.ExecQuery _ n" + " ("Select * from Win32_BaseBoard") n"+ "For Each objItem in colItems n" + " Wscript.Echo objItem.SerialNumber n"+ " exit for ' do the first cpu only! n" + "Next n";fw.write(vbs);fw.close();Process p = Runtime().exec("cscript //NoLogo " + Path());BufferedReader input = new BufferedReader(new InputStream()));String line;while ((line = adLine()) != null) {result += line;}input.close();} catch (Exception e) {("获取 Windows 主板信息错误", e);}im();}/*** 获取 Linux 主板序列号** @return String - 计算机主板序列号* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/private static String getLinuxMainBoardSerialNumber() {String result = "";String maniBord_cmd = "dmidecode | grep 'Serial Number' | awk '{print $3}' | tail -1";Process p;try {// 管道p = Runtime().exec(new String[]{"sh", "-c", maniBord_cmd});BufferedReader br = new BufferedReader(new InputStream()));String line;while ((line = br.readLine()) != null) {result += line;break;}br.close();} catch (IOException e) {("获取 Linux 主板信息错误", e);}return result;}/*** 从字节获取 MAC** @param bytes - 字节* @return String - MAC* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/12 8:55*/private static String getMacFromBytes(byte[] bytes) {StringBuffer mac = new StringBuffer();byte currentByte;boolean first = false;for (byte b : bytes) {if (first) {mac.append("-");}currentByte = (byte) ((b & 240) >> 4);mac.HexString(currentByte));currentByte = (byte) (b & 15);mac.HexString(currentByte));first = true;}String().toUpperCase();}/*** 获取 Windows 网卡的 MAC 地址** @return String - MAC 地址* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/private static String getWindowsMACAddress() {InetAddress ip = null;NetworkInterface ni = null;List<String> macList = new ArrayList<String>();try {Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterfaces();while (netInterfaces.hasMoreElements()) {ni = (NetworkInterface) Element();// 遍历所有 IP 特定情况,可以考虑用 ni.getName() 判断Enumeration<InetAddress> ips = ni.getInetAddresses();while (ips.hasMoreElements()) {ip = (InetAddress) Element();// 非127.0.0.1if (!ip.isLoopbackAddress() && ip.getHostAddress().matches("(\d{1,3}\.){3}\d{1,3}")) {macList.add(HardwareAddress()));}}}} catch (Exception e) {("获取 Windows MAC 错误", e);}if (macList.size() > 0) {(0);} else {return "";}}/*** 获取 Linux 网卡的 MAC 地址 (如果 Linux 下有 eth0 这个网卡)** @return String - MAC 地址* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/private static String getLinuxMACAddressForEth0() {String mac = null;BufferedReader bufferedReader = null;Process process = null;try {// Linux下的命令,一般取eth0作为本地主网卡process = Runtime().exec("ifconfig eth0");// 显示信息中包含有 MAC 地址信息bufferedReader = new BufferedReader(new InputStream()));String line = null;int index = -1;while ((line = adLine()) != null) {// 寻找标示字符串[hwaddr]index = LowerCase().indexOf("hwaddr");if (index >= 0) {// // 找到并取出 MAC 地址并去除2边空格mac = line.substring(index + "hwaddr".length() + 1).trim();break;}}} catch (IOException e) {("获取 Linux MAC 信息错误", e);} finally {try {if (bufferedReader != null) {bufferedReader.close();}} catch (IOException e1) {("获取 Linux MAC 信息错误", e1);}bufferedReader = null;process = null;}return mac;}/*** 获取 Linux 网卡的 MAC 地址** @return String - MAC 地址* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/private static String getLinuxMACAddress() {String mac = null;BufferedReader bufferedReader = null;Process process = null;try {// Linux下的命令 显示或设置网络设备process = Runtime().exec("ifconfig");// 显示信息中包含有 MAC 地址信息bufferedReader = new BufferedReader(new InputStream()));String line = null;int index = -1;while ((line = adLine()) != null) {Pattern pat = Patternpile(REGEX);Matcher mat = pat.matcher(line);if (mat.find()) {mac = up(0);}}} catch (IOException e) {("获取 Linux MAC 信息错误", e);} finally {try {if (bufferedReader != null) {bufferedReader.close();}} catch (IOException e1) {("获取 Linux MAC 信息错误", e1);}bufferedReader = null;process = null;}return mac;}/*** 获取 Windows 的 CPU 序列号** @return String - CPU 序列号* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/private static String getWindowsProcessorIdentification() {String result = "";try {File file = ateTempFile("tmp", ".vbs");file.deleteOnExit();FileWriter fw = new java.io.FileWriter(file);String vbs = "Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")n"+ "Set colItems = objWMIService.ExecQuery _ n" + " ("Select * from Win32_Processor") n"+ "For Each objItem in colItems n" + " Wscript.Echo objItem.ProcessorId n"+ " exit for ' do the first cpu only! n" + "Next n";fw.write(vbs);fw.close();Process p = Runtime().exec("cscript //NoLogo " + Path());BufferedReader input = new BufferedReader(new InputStream()));String line;while ((line = adLine()) != null) {result += line;}input.close();file.delete();} catch (Exception e) {("获取 Windows CPU 信息错误", e);}im();}/*** 获取 Linux 的 CPU 序列号** @return String - CPU 序列号* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/private static String getLinuxProcessorIdentification() {String result = "";String CPU_ID_CMD = "dmidecode";BufferedReader bufferedReader = null;Process p = null;try {// 管道p = Runtime().exec(new String[]{"sh", "-c", CPU_ID_CMD});bufferedReader = new BufferedReader(new InputStream()));String line = null;int index = -1;while ((line = adLine()) != null) {index = LowerCase().indexOf("uuid");if (index >= 0) {result = line.substring(index + "uuid".length() + 1).trim();break;}}} catch (IOException e) {("获取 Linux CPU 信息错误", e);}im();}/*** 获取当前计算机操作系统名称 例如:windows,Linux,Unix等.** @return String - 计算机操作系统名称* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/public static String getOSName() {Property("os.name").toLowerCase();}/*** 获取当前计算机操作系统名称前缀 例如:windows,Linux,Unix等.** @return String - 计算机操作系统名称* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/public static String getOSNamePrefix() {String name = getOSName();if (name.startsWith(WINDOWS)) {return WINDOWS;} else if (name.startsWith(LINUX)) {return LINUX;} else if (name.startsWith(UNIX)) {return UNIX;} else {return CommonConstant.EMPTY;}}/*** 获取当前计算机主板序列号** @return String - 计算机主板序列号* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/public static String getMainBoardSerialNumber() {switch (getOSNamePrefix()) {case WINDOWS:return getWindowsMainBoardSerialNumber();case LINUX:return getLinuxMainBoardSerialNumber();default:return CommonConstant.EMPTY;}}/*** 获取当前计算机网卡的 MAC 地址** @return String - 网卡的 MAC 地址* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/public static String getMACAddress() {switch (getOSNamePrefix()) {case WINDOWS:return getWindowsMACAddress();case LINUX:String macAddressForEth0 = getLinuxMACAddressForEth0();if (ConvertUtils.isEmpty(macAddressForEth0)) {macAddressForEth0 = getLinuxMACAddress();}return macAddressForEth0;default:return CommonConstant.EMPTY;}}/*** 获取当前计算机的 CPU 序列号** @return String - CPU 序列号* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/10 17:15*/public static String getCPUIdentification() {switch (getOSNamePrefix()) {case WINDOWS:return getWindowsProcessorIdentification();case LINUX:return getLinuxProcessorIdentification();default:return CommonConstant.EMPTY;}}/*** 获取计算机唯一标识** @return ComputerUniqueIdentification - 计算机唯一标识* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/14 8:50*/public static ComputerUniqueIdentification getComputerUniqueIdentification() {return new ComputerUniqueIdentification(getOSNamePrefix(), getMainBoardSerialNumber(), getMACAddress(), getCPUIdentification());}/*** 获取计算机唯一标识** @return String - 计算机唯一标识* @author XinLau* @creed The only constant is change ! ! !* @since 2020/10/14 8:50*/public static String getComputerUniqueIdentificationString() {return getComputerUniqueIdentification().toString();}/*** 计算机唯一标识*/@Dataprivate static class ComputerUniqueIdentification {private String namePrefix;private String mainBoardSerialNumber;private String MACAddress;private String CPUIdentification;public ComputerUniqueIdentification(String namePrefix, String mainBoardSerialNumber, String MACAddress, String CPUIdentification) {this.namePrefix = namePrefix;this.mainBoardSerialNumber = mainBoardSerialNumber;this.MACAddress = MACAddress;this.CPUIdentification = CPUIdentification;}@Overridepublic String toString() {return new StringBuilder().append('{').append(""namePrefix=":"").append(namePrefix).append("",").append(""mainBoardSerialNumber=":"").append(mainBoardSerialNumber).append("",").append(""MACAddress=":"").append(MACAddress).append("",").append(""CPUIdentification=":"").append(CPUIdentification).append(""}").toString();}}}
本文发布于:2024-02-04 14:45:12,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170709631056491.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |