免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4978 | 回复: 4
打印 上一主题 下一主题

java 读取linux下的硬盘序列号问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-25 15:09 |只看该作者 |倒序浏览
想通过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类有这个必要吗?

论坛徽章:
6
CU大牛徽章
日期:2013-04-17 10:59:39CU大牛徽章
日期:2013-04-17 11:01:45CU大牛徽章
日期:2013-04-17 11:02:15CU大牛徽章
日期:2013-04-17 11:02:36CU大牛徽章
日期:2013-04-17 11:02:582015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2008-08-25 16:27 |只看该作者
exec方式移植性不好.没办法.

论坛徽章:
0
3 [报告]
发表于 2008-08-25 16:37 |只看该作者
这个根据系统不同而不同很难使用一个方法去实现吧

论坛徽章:
0
4 [报告]
发表于 2008-08-26 12:37 |只看该作者
系统得有c的运行环境,系统就是C编译的,还什么C的运行环境

论坛徽章:
4
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11IT运维版块每日发帖之星
日期:2016-08-11 06:20:00IT运维版块每日发帖之星
日期:2016-08-15 06:20:00
5 [报告]
发表于 2008-08-26 14:28 |只看该作者

回复 #1 paradise2009 的帖子

IDE硬盘才有序列号,SCSI硬盘应该是没有序列号的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP