Java获取硬盘序列号

3

我正在尝试制作一个Java程序,通过cmd或终端获取硬盘序列号。我在Windows和Linux操作系统上都进行了尝试。但是在Linux上我遇到了问题,当我在终端中键入hdparm -I /dev/sda | grep Serial时,它会返回一个空格,而不是显示硬盘序列号。

我的问题是,如何获取或显示硬盘序列号。

以下是我的代码:

    private static String OS= System.getProperty("os.name").toLowerCase();
    private static String system;
    private static String serial;
    private void getVol(String drive)
    {
        String query=new String();
        if(isWindows())
        {
            query="cmd /c"+" vol "+drive+":";
        }
        else if(isUnix())
        {
            query="hdparm -I /dev/sda | grep Serial";
        }
        try
        {
            Runtime rt=Runtime.getRuntime();
            InputStream is=rt.exec(query).getInputStream();
            InputStreamReader isr=new InputStreamReader(is);
            BufferedReader br=new BufferedReader(isr);
            String line;
            if(isWindows())
            {
                br.readLine();
                line=br.readLine();
                line=line.substring(line.lastIndexOf(" ")+1);
                serial=line;
            }
            else if(isUnix())
            {
                line=br.readLine();
                serial=line;
            }
        }catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }

    private static boolean isWindows() {
        return (OS.indexOf("win") >= 0);
    }
    private static boolean isUnix()
    {
        return (OS.indexOf("nux") >= 0);
    }

    public static void main(String args[])
    {
        MainClass f=new MainClass();
        f.getVol("C");
        System.out.println(serial);
    }

它在Windows中返回Windows 8.1,并在CentOS(Linux)中返回Linux。 - askManiac
@giusc 抱歉,我对Linux完全不熟悉...你能帮我吗?如何使用其绝对路径调用hdparm? - askManiac
@askManiac 在Linux中,通常可以在/sbin/hdparm下找到hdparm,因此您可以尝试在查询变量中更改它。此外,请参考本文来解决Runtime.exec()问题。 - giusc
2
@askManiac 还要注意,您正在尝试使用 shell 命令,因此需要稍微更改代码:String yourShellCommand = "/sbin/hdparm -I /dev/sda | grep Serial"; String[] commandAndArgs = new String[]{ "/bin/sh", "-c", yourShellCommand }; Runtime.getRuntime().exec(commandAndArgs); - giusc
1
不要在评论中提供澄清。编辑您的问题以使其更清晰。 - Raedwald
显示剩余9条评论
2个回答

2

这篇文章发布已经有一段时间了,但是@giusc的解决方案对我不起作用,所以在此分享我的研究成果。

在Linux上获取硬盘序列号:

public static void main(String[] args) throws Exception {

    String sc = "/sbin/udevadm info --query=property --name=sda"; // get HDD parameters as non root user
    String[] scargs = {"/bin/sh", "-c", sc};

    Process p = Runtime.getRuntime().exec(scargs);
    p.waitFor();

    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 
    String line;
    StringBuilder sb  = new StringBuilder();

    while ((line = reader.readLine()) != null) {
        if (line.indexOf("ID_SERIAL_SHORT") != -1) { // look for ID_SERIAL_SHORT or ID_SERIAL
            sb.append(line);
        }    
    }

    System.out.println("HDD Serial number:" + sb.toString().substring(sb.toString().indexOf("=") + 1));
}

获取硬盘序列号(制造商)Windows:

public static void main(String[] args) {
    String sc = "cmd /c" + "wmic diskdrive get serialnumber";

    Process p = Runtime.getRuntime().exec(sc);
    p.waitFor();

    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String line;
    StringBuilder sb = new StringBuilder();

    while ((line = reader.readLine()) != null) {
        sb.append(line);
    } 

    System.out.println("HDD Serial number: " + sb.substring(sb.toString().lastIndexOf("r") + 1).trim());
}

0

你的问题几乎肯定是运行hdparm需要root特权。它不能由普通用户运行。

此外,你在Windows上读取的“序列号”是主文件系统的卷序列号,而不是硬盘的序列号。它可以相当容易地更改(例如,使用VolumeID)。


如何在非root用户下运行,请参考http://stackoverflow.com/questions/7619483/getting-hdd-serial-on-linux-without-root-permissions。 - Raedwald

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接