获取硬盘的序列号

package com.uniware.license.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * Description:获取各个操作系统下硬盘序列号
 */
public class HDUtil {

    /**
     * Return Opertaion System Name;
     *
     * @return os name.
     */
    public static String getOsName() {
        String os = "";
        os = System.getProperty("os.name");
        return os;
    }

    /**
     * Returns the HD SerialNo. of the computer.
     *
     * @return the HD SerialNo.
     */
    public static String getHDSerialNo(String driver) {
        String sn = "";
        String os = getOsName().toLowerCase();
        if (os.startsWith("linux")) {
            sn = getLinuxSerialNumber(driver);
        } else if (os.startsWith("win")) {
            sn = getWinSerialNumber(driver);
        }
        sn = sn.trim();
        return sn;
    }

    public static String getLinuxSerialNumber(String driver) {
        String sn = "";
        if (isSCSIorIDEHD() == "scsi") {
            // 注意如果是ubuntu等系统用户,本身没有root权限,请先:chmod 777 /dev/sda
            // String command = "hdparm -i /dev/sda";
            String command = "hdparm -i " + driver;
            Process p;
            try {
                p = Runtime.getRuntime().exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    if (line.contains("SerialNo")) {
                        int index = line.indexOf("SerialNo")
                                + "SerialNo".length() + 1;
                        sn = line.substring(index);
                        break;
                    }
                }
                br.close();
            } catch (IOException e) {
            }
        } else if (isSCSIorIDEHD() == "ide") {
            // 注意如果是ubuntu等系统用户,本身没有root权限,请先:chmod 777 /dev/sda
            // String command = "hdparm -i /dev/hda";
            String command = "hdparm -i " + driver;
            Process p;
            try {
                p = Runtime.getRuntime().exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    if (line.contains("SerialNo")) {
                        int index = line.indexOf("SerialNo")
                                + "SerialNo".length() + 1;
                        sn = line.substring(index);
                        break;
                    }
                }
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sn;
    }

    /**
     * 获得windows的磁盘序列号
     *
     * @param drive
     * @return
     */
    public static String getWinSerialNumber(String drive) {
        String result = "";
        String temp = "c:\\temp";
        try {
            File tempFile = new File(temp);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            File file = new File(tempFile,"realhowto.vbs");
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);
            String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                    + "Set colDrives = objFSO.Drives\n"
                    + "Set objDrive = colDrives.item(\""
                    + drive
                    + "\")\n"
                    + "Wscript.Echo objDrive.SerialNumber"; // see note
            fw.write(vbs);
            fw.close();
            Process p = Runtime.getRuntime().exec(
                    "cscript //NoLogo " + file.getPath());
            BufferedReader input = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.trim();
    }

    public static String isSCSIorIDEHD() {
        String os = getOsName().toLowerCase();
        if (os.startsWith("linux")) {
            // ubuntu系统下确定有root权限
            String command = "fdisk -l";
            Process p;
            try {
                p = Runtime.getRuntime().exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    if (line.contains("sd")) {
                        return "scsi";
                    }
                    if (line.contains("hd")) {
                        return "ide";
                    }
                }
                br.close();
            } catch (IOException e) {
            }
        }
        return "unkonwn"; // 未知类型
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值