So I'm in need of some people to help me test this to make sure it fetches a serial number properly on all operating systems. This has been tested on windows 10 and mac os x and worked but I'm not sure about linux or windows 7/8. If you could simply run this class and let me know if it prints out the serial number that would be awesome.
Code:
package com;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Testing {
public static String serialNumber;
public static Platform platform;
/**
* This Platform enum contains the different operative systems we are
* expecting to deal with.
*/
private static enum Platform {
LINUX, MAC_OS_X, UNKOWN, WINDOWS;
}
/**
* Internal function to determine the {@code Platform} type.
*
* @return the {@code Platform} this machine is running.
*/
private static Platform getPlatform() {
final String name = System.getProperty("os.name").toLowerCase();
if (name.contains("win")) {
return Platform.WINDOWS;
} else if (name.contains("mac")) {
return Platform.MAC_OS_X;
} else if (name.contains("linux")) {
return Platform.LINUX;
} else if (name.contains("unix")) {
return Platform.LINUX;
}
return Platform.UNKOWN;
}
public static String getSerialNumber() {
String sn = null;
OutputStream os = null;
InputStream is = null;
Runtime runtime = Runtime.getRuntime();
Process process = null;
switch (platform) {
case MAC_OS_X:
try {
process = runtime.exec(new String[] { "/usr/sbin/system_profiler", "SPHardwareDataType" });
} catch (IOException e) {
throw new RuntimeException(e);
}
os = process.getOutputStream();
is = process.getInputStream();
try {
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
String marker = "Serial Number";
try {
while ((line = br.readLine()) != null) {
if (line.contains(marker)) {
sn = line.split(":")[1].trim();
break;
}
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (sn == null) {
throw new RuntimeException("Cannot find computer SN");
}
return sn;
case LINUX:
if (sn == null) {
sn = readDmidecode();
}
if (sn == null) {
sn = readLshal();
}
if (sn == null) {
throw new RuntimeException("Cannot find computer SN");
}
return sn;
case WINDOWS:
sn = "";
// SN of motherboard
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 l;
while ((l = input.readLine()) != null) {
sn += l;
}
input.close();
// SN of C drive (shouldn't fail, hopefully...)
if (sn.isEmpty()) {
file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
fw = new java.io.FileWriter(file);
vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n" + "Set colDrives = objFSO.Drives\n" + "Set objDrive = colDrives.item(\"C\")\n"
+ "Wscript.Echo objDrive.SerialNumber"; // see note
fw.write(vbs);
fw.close();
p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((l = input.readLine()) != null) {
sn += l;
}
input.close();
}
// SN of bios
if (sn.isEmpty()) {
process = Runtime.getRuntime().exec(new String[] { "wmic", "bios", "get", "serialnumber" });
process.getOutputStream().close();
Scanner sc = new Scanner(process.getInputStream());
String property = sc.next();
String serial = sc.next();
sn = serial;
sc.close();
}
if (sn.isEmpty()) {
throw new RuntimeException("Unable to find SN");
}
return sn;
} catch (Exception e) {
e.printStackTrace();
}
return sn;
default:
return "empty_serial";
}
}
private static String readDmidecode() {
String line = null;
String marker = "Serial Number:";
BufferedReader br = null;
String serial = null;
try {
br = read("dmidecode -t system");
while ((line = br.readLine()) != null) {
if (line.indexOf(marker) != -1) {
serial = line.split(marker)[1].trim();
break;
}
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return serial;
}
private static String readLshal() {
String line = null;
String marker = "system.hardware.serial =";
BufferedReader br = null;
String serial = null;
try {
br = read("lshal");
while ((line = br.readLine()) != null) {
if (line.indexOf(marker) != -1) {
serial = line.split(marker)[1].replaceAll("\\(string\\)|(\\')", "").trim();
break;
}
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return serial;
}
private static BufferedReader read(String command) {
OutputStream os = null;
InputStream is = null;
Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
process = runtime.exec(command.split(" "));
} catch (IOException e) {
throw new RuntimeException(e);
}
os = process.getOutputStream();
is = process.getInputStream();
try {
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return new BufferedReader(new InputStreamReader(is));
}
public static void main(String[] args) {
platform = getPlatform();
serialNumber = getSerialNumber();
final String name = System.getProperty("os.name").toLowerCase();
final String version = System.getProperty("os.version").toLowerCase();
JOptionPane.showMessageDialog(null, "OS: " + name + ", Version: " + version + ", Serial: " + serialNumber);
}
}