2014-09-01 129 views
2

我想制作一个java程序,它将通过cmd或终端的硬盘的序列。我试图在操作系统(Windows,Linux)上。我遇到了Linux的问题,它会返回一个空格,当我在终端中键入hdparm -I /dev/sda | grep Serial它显示硬盘的序列号。java越来越硬盘序列

问题如何获取或显示序列号。

这里是我的代码:

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); 
    } 
+0

你从'系统获得什么样的价值。 。的getProperty( “os.name”)toLowerCase();'? – 2014-09-01 06:06:35

+0

它在windows中返回windows 8.1并且在centOS(linux)中返回linux – askManiac 2014-09-01 06:07:57

+0

在你的Linux操作系统上,我的意思是。那是给你的问题,对吧? – 2014-09-01 06:10:16

回答

0

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

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

+0

以及我目前正在以root身份运行,但仍然无法正常工作... – askManiac 2014-09-01 06:45:23

+0

有关如何将其作为非'rootro'运行,请参阅http://stackoverflow.com/questions/7619483/getting-hdd-serial-上Linux的无根的权限 – Raedwald 2014-09-01 06:51:31

0

自发布以来已有一段时间了,但@ 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()); 
}