- 论坛徽章:
- 0
|
想通过java写一个读取安装在linux系统上的硬盘的序列号的util类。
我写的代码是:
1. import java.io.BufferedReader;
2. import java.io.IOException;
3. import java.io.InputStreamReader;
4. import java.util.ArrayList;
5. import java.util.List;
6.
7. /**
8. *
9. * Description: 获取各个操作系统下硬盘序列号
10. *
11. *
12. * @version 1.0 Aug 25, 2008 12:12:36 PM
13. *
14. */
15.
16. public class HDUtil {
17.
18. /**
19. * Return Opertaion System Name;
20. *
21. * @return os name.
22. */
23. public static String getOsName() {
24. String os = "";
25. os = System.getProperty("os.name");
26. return os;
27. }
28.
29. /**
30. * Returns the HD SerialNo. of the computer.
31. *
32. * @return the HD SerialNo.
33. */
34. public static String getHDSerialNo() {
35. String sn = "";
36. String os = getOsName();
37. if (os.startsWith("Linux")) {
38. if (isSCSIorIDEHD() == "scsi") {
39. // 注意如果是ubuntu等系统用户,本身没有root权限,请先:chmod 777 /dev/sda
40. String command = "hdparm -i /dev/sda";
41. Process p;
42. try {
43. p = Runtime.getRuntime().exec(command);
44. BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
45. String line;
46. while ((line = br.readLine()) != null) {
47. if (line.contains("SerialNo")) {
48. int index = line.indexOf("SerialNo") + "SerialNo".length() + 1;
49. sn = line.substring(index);
50. break;
51. }
52. }
53. br.close();
54. } catch (IOException e) {
55. }
56. } else if (isSCSIorIDEHD() == "ide") {
57. // 注意如果是ubuntu等系统用户,本身没有root权限,请先:chmod 777 /dev/sda
58. String command = "hdparm -i /dev/hda";
59. Process p;
60. try {
61. p = Runtime.getRuntime().exec(command);
62. BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
63. String line;
64. while ((line = br.readLine()) != null) {
65. if (line.contains("SerialNo")) {
66. int index = line.indexOf("SerialNo") + "SerialNo".length() + 1;
67. sn = line.substring(index);
68. break;
69. }
70. }
71. br.close();
72. } catch (IOException e) {
73. }
74. } else {
75. sn = "unknown";
76. }
77.
78. }
79. sn = sn.trim();
80. return sn;
81. }
82.
83. public static String isSCSIorIDEHD() {
84. String os = getOsName();
85. if (os.startsWith("Linux")) {
86. // ubuntu系统下确定有root权限
87. String command = "fdisk -l";
88. Process p;
89. try {
90. p = Runtime.getRuntime().exec(command);
91. BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
92. String line;
93. while ((line = br.readLine()) != null) {
94. if (line.contains("sd")) {
95. return "scsi";
96. }
97. if (line.contains("hd")) {
98. return "ide";
99. }
100. }
101. br.close();
102. } catch (IOException e) {
103. }
104. }
105. return "unkonwn"; // 未知类型
106. }
107.
108. /**
109. * Main Class.
110. *
111. * @param args
112. */
113. public static void main(String[] args) {
114. System.out.println("Operation System=" + getOsName());
115. System.out.println("HD SerialNo=" + getHDSerialNo());
116. }
117. }
现在在本地机器ubuntu上可以运行,但是放到其他比如Centos或suse上获取不了。
根本原因是通过java调用的一个SHELL>"hdparm -i /dev/sda1" ,这个命令对于SCSI 的硬盘好像支持不了。
有谁做过的给个建议阿。好多人说用c写个底层的,然后通过jni调用。那样不是系统得有c的运行环境吗?
我觉得一个util类有这个必要吗? |
|