java - 如何在 Java 中查找 Linux 操作系统的 UUID 和序列号
问题描述
我正在尝试查找 linux 操作系统的 UUID。下面是我正在尝试的代码,它给出了空值..
public static String getUUIDForLinux() {
StringBuffer output = new StringBuffer();
Process process;
String[] cmd = {"/bin/sh", "-c", "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) {
log.error(e.getMessage(),e);
}
log.info("uuid for linux:"+output.toString());
return output.toString();
}
使用以下代码获取序列号。我在这段代码中做错了什么..
public static String getSerialNumHDForLinux() {
String machineIdLinux = "";
try {
StringBuffer output = new StringBuffer();
Process p = Runtime.getRuntime().exec("/bin/bash -c\"hdparm -I /dev/sda | grep Serial\"");
BufferedReader sNumReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = sNumReader.readLine()) != null) {
output.append(line + "\n");
}
machineIdLinux =output.toString().substring(output.indexOf("\n"), output.length()).trim();
}catch(IOException e) {
log.error(e.getMessage());
}
return machineIdLinux;
}
有人请帮我更正代码..
解决方案
此代码适用于获取 linux 机器的 UUID。
public static String getUUIDForLinux() {
StringBuffer output = new StringBuffer();
Process process;
try {
process = Runtime.getRuntime().exec("cat /sys/class/dmi/id/product_uuid");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
log.error(e.getMessage(),e);
}
log.info("uuid for linux:"+output.toString());
return output.toString();
}
为了根据我们的服务器获取序列号,我使用了以下命令:上述代码中的 cat /sys/class/dmi/id/product_serial。
推荐阅读
- c# - 事件目标 C#
- selenium - org.openqa.selenium.ElementNotInteractableException: Element could not be scrolled into view when trying to click a button
- r - 在 R 中使用 system2() 运行编译的 cpp 文件
- drupal - 遍历所有页面并在单个 html 文件中输出
- mysql - 我使用 python 从 MySQL 数据库中检索数据,但未显示属性名称。如何以 python 字典格式显示数据?
- alfresco - 露天。如何按条件显示表单域?
- javascript - 如何对输入类型范围进行分组并保持 100% 组合在一起的所有范围的最大值
- xml - 努力将 Powershell XML 转换为 CSV
- python-3.x - 如何获取目录python中包含特定字符串的子目录中文件的路径
- python - Python 3 - urllib.request - HTTPError