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"; // 未知类型
}
}
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"; // 未知类型
}
}