Como obter o serial do disco rigido em Java?
Dei uma pesquisada no forum da sun.
E eles dizem que nao eh possivel em java provavelmente vai ter que usar JNI.
Procure um programa chamado “Smartapp”.
A saída desse programa no meu computador é:
Physical drive opened successfully
DFP_GET_VERSION returned:
bVersion = 1
bRevision = 1
fCapabilities = 0x7
bReserved = 0x0
bIDEDeviceMap = 0x1
cbBytesReturned = 24
SMART Enabled on Drive: 0
cbBytesReturned: 16
Drive 0 is an IDE Hard drive that supports SMART
#Cylinders: 16383, #Heads: 16, #Sectors per Track: 63
IDE TASK FILE REGISTERS:
bFeaturesReg = 0x0
bSectorCountReg = 0x1
bSectorNumberReg = 0x1
bCylLowReg = 0x0
bCylHighReg = 0x0
bDriveHeadReg = 0xA0
Status = 0xEC
Model number: SAMSUNG HD000XX/Q
Firmware rev: ZM100-34
Serial number: S0VRJ2KP123456
cbBytesReturned: 528
Data for Drive Number 0
Attribute Structure Revision Threshold Structure Revision
16 16
-Attribute Name- -Attribute Value- -Threshold Value-
1 Raw Read Error Rate 100 51
3 Spin Up Time 100 25
4 Start/Stop Count 100 0
5 Reallocated Sector Count 253 10
7 Seek Error Rate 253 0
8 Seek Time Performance 253 0
9 Power On Hours Count 100 0
A Spin Retry Count 253 0
B Calibration Retry Count 253 0
C Power Cycle Count 100 0
BE (Unknown attribute) 64 0
C2 (Unknown attribute) 64 0
C3 (Unknown attribute) 100 0
C4 (Unknown attribute) 253 0
C5 (Unknown attribute) 253 0
C6 (Unknown attribute) 253 0
C7 (Unknown attribute) 200 0
C8 (Unknown attribute) 100 0
C9 (Unknown attribute) 100 0
CA (Unknown attribute) 253 0
Uma das linhas indica qual é o número de série do disco (que neste caso é “S0VRJ2KP123456”).
queria mesmo era obter o serial dentro de uma aplicação java… preciso de um exemplo do codigo
a) Arranje o programa smartapp.exe
b) Use Runtime.exec para executar esse programa, e pegar sua saída
c) Para usar Runtime.exec corretamente, leia o artigo:
Sei que o post é bem antigo.
porém segue uma classe que atende a necessidade.
Abraços
public class InfoSis {
private final static String getHDSerial() throws IOException {
String os = System.getProperty("os.name");
try {
if (os.startsWith("Windows")) {
return getHDSerialWindows("C");
} else if (os.startsWith("Linux")) {
return getHDSerialLinux();
} else {
throw new IOException("unknown operating system: " + os);
}
} catch (Exception ex) {
ex.printStackTrace();
throw new IOException(ex.getMessage());
}
}
private final static String getCPUSerial() throws IOException {
String os = System.getProperty("os.name");
try {
if (os.startsWith("Windows")) {
return getCPUSerialWindows();
} else if (os.startsWith("Linux")) {
return getCPUSerialLinux();
} else {
throw new IOException("unknown operating system: " + os);
}
} catch (Exception ex) {
ex.printStackTrace();
throw new IOException(ex.getMessage());
}
}
private final static String getMotherboardSerial() throws IOException {
String os = System.getProperty("os.name");
try {
if (os.startsWith("Windows")) {
return getMotherboardSerialWindows();
} else if (os.startsWith("Linux")) {
return getMotherboardSerialLinux();
} else {
throw new IOException("unknown operating system: " + os);
}
} catch (Exception ex) {
ex.printStackTrace();
throw new IOException(ex.getMessage());
}
}
// Implementacoes
/*
* Captura serial de placa mae no WINDOWS, atraves da execucao de script
* visual basic
*/
public static String getMotherboardSerialWindows() {
String result = "";
try {
File file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.SerialNumber \n"
+ " exit for ' do the first cpu only! \n" + "Next \n";
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();
}
/*
* Captura serial de placa mae em sistemas LINUX, atraves da execucao de
* comandos em shell.
*/
public static String getMotherboardSerialLinux() {
String result = "";
try {
String[] args = {"bash", "-c", "lshw -class bus | grep serial"};
Process p = Runtime.getRuntime().exec(args);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch (Exception e) {
}
if (result.trim().length() < 1 || result == null) {
result = "NO_DISK_ID";
}
return filtraString(result, "serial: ");
}
/*
* Captura serial de HD no WINDOWS, atraves da execucao de script visual
* basic
*/
public static String getHDSerialWindows(String drive) {
String result = "";
try {
File file = File.createTempFile("tmp", ".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";
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) {
}
if (result.trim().length() < 1 || result == null) {
result = "NO_DISK_ID";
}
return result.trim();
}
/*
* Captura serial de HD em sistemas Linux, atraves da execucao de comandos
* em shell.
*/
public static String getHDSerialLinux() {
String result = "";
try {
String[] args = {"bash", "-c", "lshw -class disk | grep serial"};
Process p = Runtime.getRuntime().exec(args);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch (Exception e) {
}
if (result.trim().length() < 1 || result == null) {
result = "NO_DISK_ID";
}
return filtraString(result, "serial: ");
}
/*
* Captura serial da CPU no WINDOWS, atraves da execucao de script visual
* basic
*/
public static String getCPUSerialWindows() {
String result = "";
try {
File file = File.createTempFile("tmp", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "On Error Resume Next \r\n\r\n"
+ "strComputer = \".\" \r\n"
+ "Set objWMIService = GetObject(\"winmgmts:\" _ \r\n"
+ " & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\") \r\n"
+ "Set colItems = objWMIService.ExecQuery(\"Select * from Win32_Processor\") \r\n "
+ "For Each objItem in colItems\r\n "
+ " Wscript.Echo objItem.ProcessorId \r\n "
+ " exit for ' do the first cpu only! \r\n"
+ "Next ";
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) {
}
if (result.trim().length() < 1 || result == null) {
result = "NO_CPU_ID";
}
return result.trim();
}
/*
* Captura serial de CPU em sistemas Linux, atraves da execucao de comandos
* em shell.
*/
public static String getCPUSerialLinux() {
String result = "";
try {
String[] args = {"bash", "-c", "lshw -class processor | grep serial"};
Process p = Runtime.getRuntime().exec(args);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch (Exception e) {
}
if (result.trim().length() < 1 || result == null) {
result = "NO_DISK_ID";
}
return filtraString(result, "serial: ");
}
public static String filtraString(String nome, String delimitador) {
return nome.split(delimitador)[1];
}
public static String retornaMacAddress() throws SocketException, UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
NetworkInterface netInter = NetworkInterface.getByInetAddress(localHost);
byte[] macAddressBytes = netInter.getHardwareAddress();
String macAddress = String.format(
"%1$02x-%2$02x-%3$02x-%4$02x-%5$02x-%6$02x",
macAddressBytes[0], macAddressBytes[1], macAddressBytes[2],
macAddressBytes[3], macAddressBytes[4], macAddressBytes[5]).toUpperCase();
return macAddress;
}
public static void main(String[] args) {
try {
System.out.println("Serial do HD: " + getHDSerial());
System.out.println("Serial da CPU: " + getCPUSerial());
System.out.println("Serial da Placa Mae: " + getMotherboardSerial());
System.out.println("Serial do MAC: " + retornaMacAddress());
} catch (IOException e) {
e.printStackTrace();
}
}
}
1 curtida