如何使用 Java 获取计算机特定的 ID 号
How to get a computer specific ID number using Java
我想使用 MAC 地址以外的机器特定 ID 号创建 UUID 哈希,以验证运行 java 应用程序的计算机。是否可以使用 Java 1.8?如果是这样,我可以选择的最佳选择是什么?如果它同时用于 Windows 和 Unix 平台,将会更有帮助。
是的。您可以在 PC 和 linux 系统中不使用 MAC 地址。
我将逐步打破这个过程。
第 1 步:识别 OS
在您的 java 代码中,确定像这样使用的 OS
private static String OS = System.getProperty("os.name").toLowerCase();
if(OS.indexOf("win") >= 0)
//your code for windows OS.
else if(OS.indexOf("mac") >= 0)
//your code for MAC OS.
else if(OS.indexOf("sunos") >= 0)
//your code for Solaris OS
else if(OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 )
//your code for unix OS's
步骤 2:使用所需命令获取系统的 UUID
什么是 UUID?
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems.
对于windows
Runtime.exec("wmic csproduct get UUID");
cmd命令 wmic csproduct get UUID
returns PC的UUID [windows]
对于Linux
将此内核命令与 Runtime.exec("YOUR COMMAND")
一起使用
# cat /sys/class/dmi/id/product_uuid
要了解有关 Runtime.exec
的更多信息,请查看此 java.lang.Runtime.exec
java.lang.Runtime.exec : 通过这种方式,您可以为任何底层环境提供适当的 shell 命令,无论是 MAC、Windows、Linux等
就像这样,可以根据平台将 UUID 作为字符串检索。
String OS = System.getProperty("os.name").toLowerCase();
String machineId = null;
if (OS.indexOf("win") >= 0) {
StringBuffer output = new StringBuffer();
Process process;
String[] cmd = {"wmic", "csproduct", "get", "UUID"};
try {
process = Runtime.getRuntime().exec(cmd);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
machineId = output.toString();
} else if (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0) {
StringBuffer output = new StringBuffer();
Process process;
String[] cmd = {"/bin/sh", "-c", "echo <password for superuser> | sudo -S cat /sys/class/dmi/id/product_uuid"};
try {
process = Runtime.getRuntime().exec(cmd);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
machineId = output.toString();
}
是...您可以使用 Java 获取计算机特定的 ID 号
您可以使用系统的 UUID 作为您计算机的特定 ID。
使用 java 获取系统的 UUID。请参考以下代码:-
String command = "wmic csproduct get UUID";
StringBuffer output = new StringBuffer();
Process SerNumProcess = Runtime.getRuntime().exec(command);
BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));
String line = "";
while ((line = sNumReader.readLine()) != null) {
output.append(line + "\n");
}
String MachineID=output.toString().substring(output.indexOf("\n"), output.length()).trim();;
System.out.println(MachineID);
}
但是您只能使用此代码获取 Windows 系统的 UUID。
如果您想使用 java 获取 MAC os 的 UUID。
参考这段代码:
String command = "system_profiler SPHardwareDataType | awk '/UUID/ { print ; }'";
StringBuffer output = new StringBuffer();
Process SerNumProcess = Runtime.getRuntime().exec(command);
BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));
String line = "";
while ((line = sNumReader.readLine()) != null) {
output.append(line + "\n");
}
String MachineID=output.toString().substring(output.indexOf("UUID: "), output.length()).replace("UUID: ", "");
SerNumProcess.waitFor();
sNumReader.close();
System.out.println(MachineID);
}
谢谢。
我想使用 MAC 地址以外的机器特定 ID 号创建 UUID 哈希,以验证运行 java 应用程序的计算机。是否可以使用 Java 1.8?如果是这样,我可以选择的最佳选择是什么?如果它同时用于 Windows 和 Unix 平台,将会更有帮助。
是的。您可以在 PC 和 linux 系统中不使用 MAC 地址。
我将逐步打破这个过程。
第 1 步:识别 OS
在您的 java 代码中,确定像这样使用的 OS
private static String OS = System.getProperty("os.name").toLowerCase();
if(OS.indexOf("win") >= 0)
//your code for windows OS.
else if(OS.indexOf("mac") >= 0)
//your code for MAC OS.
else if(OS.indexOf("sunos") >= 0)
//your code for Solaris OS
else if(OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 )
//your code for unix OS's
步骤 2:使用所需命令获取系统的 UUID
什么是 UUID?
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems.
对于windows
Runtime.exec("wmic csproduct get UUID");
cmd命令 wmic csproduct get UUID
returns PC的UUID [windows]
对于Linux
将此内核命令与 Runtime.exec("YOUR COMMAND")
一起使用
# cat /sys/class/dmi/id/product_uuid
要了解有关 Runtime.exec
的更多信息,请查看此 java.lang.Runtime.exec
java.lang.Runtime.exec : 通过这种方式,您可以为任何底层环境提供适当的 shell 命令,无论是 MAC、Windows、Linux等
就像这样,可以根据平台将 UUID 作为字符串检索。
String OS = System.getProperty("os.name").toLowerCase();
String machineId = null;
if (OS.indexOf("win") >= 0) {
StringBuffer output = new StringBuffer();
Process process;
String[] cmd = {"wmic", "csproduct", "get", "UUID"};
try {
process = Runtime.getRuntime().exec(cmd);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
machineId = output.toString();
} else if (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0) {
StringBuffer output = new StringBuffer();
Process process;
String[] cmd = {"/bin/sh", "-c", "echo <password for superuser> | sudo -S cat /sys/class/dmi/id/product_uuid"};
try {
process = Runtime.getRuntime().exec(cmd);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
machineId = output.toString();
}
是...您可以使用 Java 获取计算机特定的 ID 号 您可以使用系统的 UUID 作为您计算机的特定 ID。
使用 java 获取系统的 UUID。请参考以下代码:-
String command = "wmic csproduct get UUID";
StringBuffer output = new StringBuffer();
Process SerNumProcess = Runtime.getRuntime().exec(command);
BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));
String line = "";
while ((line = sNumReader.readLine()) != null) {
output.append(line + "\n");
}
String MachineID=output.toString().substring(output.indexOf("\n"), output.length()).trim();;
System.out.println(MachineID);
}
但是您只能使用此代码获取 Windows 系统的 UUID。
如果您想使用 java 获取 MAC os 的 UUID。 参考这段代码:
String command = "system_profiler SPHardwareDataType | awk '/UUID/ { print ; }'";
StringBuffer output = new StringBuffer();
Process SerNumProcess = Runtime.getRuntime().exec(command);
BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));
String line = "";
while ((line = sNumReader.readLine()) != null) {
output.append(line + "\n");
}
String MachineID=output.toString().substring(output.indexOf("UUID: "), output.length()).replace("UUID: ", "");
SerNumProcess.waitFor();
sNumReader.close();
System.out.println(MachineID);
}
谢谢。