Windows:
Package cn.net.comsys.helper.system.info; import Java. io. bufferedreader; import Java. io. file; import Java. io. filewriter; import Java. io. inputstreamreader; import Java. io. linenumberreader; public class hardwareutils {/*** obtain the motherboard serial number ** @ return */public static string getmotherboardsn () {string result = ""; try {file = file. createtempfile ("realhowto ",". vbs "); file. deleteonexit (); filewriter fw = new Java. Io. filewriter (File); string vbs = "set ob1_miservice = GetObject (\" winmgmts :\\\\. \ Root \ cimv2 \ ") \ n" + "set colitems = ob1_miservice. 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(cmd.exe C ("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 ();}/*** obtain the hard disk serial number ** @ Param drive * drive letter * @ return */public static string getharddisksn (string drive) {string result = ""; try {file = file. createtempfile ("realhowto ",". vbs "); file. deleteonexit (); filewriter fw = new Java. io. filewriter (File); string vbs = "set objfso = Createobject (\" scripting. file Systemobject \ ") \ n" + "set coldrives = objfso. drives \ n "+" set objdrive = coldrives. item (\ "" + drive + "\") \ n "+" wscript. echo objdrive. serialnumber "; // see notefw. write (vbs); FW. close (); process P = runtime.getruntime(cmd.exe C ("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 ();}/*** obtain the CPU serial number ** @ return */public static string getcpuserial () {string result = ""; try {file = file. createtempfile ("tmp ",". vbs "); file. deleteonexit (); filewriter fw = new Java. io. filewriter (File); string vbs = "set ob1_miservice = GetObject (\" winmgmts :\\\\. \ Root \ cimv2 \ ") \ n" + "set colitems = Obw.miservice. execquery _ \ n "+" (\ "select * From win32_processor \") \ n "+" for each objitem in colitems \ n "+" wscript. echo objitem. processorid \ n "+" exit for 'Do the first CPU only! \ N "+" next \ n "; // +" exit for \ r \ n "+" Next "; FW. write (vbs); FW. close (); process P = runtime.getruntime(cmd.exe C ("cscript // nologo" + file. getpath (); bufferedreader input = new bufferedreader (New inputstreamreader (P. getinputstream (); string line; while (line = input. readline ())! = NULL) {result + = line;} input. close (); file. delete ();} catch (exception e) {e. fillinstacktrace ();} If (result. trim (). length () <1 | result = NULL) {result = "No cpu_id is read";} return result. trim ();}/*** get MAC address */public static string getmac () {string result = ""; try {Process = runtime.getruntime(cmd.exe C ("ipconfig/all"); inputstreamreader IR = new inputstreamreader (process. getinputstream (); line Numberreader input = new linenumberreader (IR); string line; while (line = input. Readline ())! = NULL) if (line. indexof ("physical address")> 0) {string macaddr = line. substring (line. indexof ("-")-2); Result = macaddr ;}} catch (Java. io. ioexception e) {system. err. println ("ioexception" + E. getmessage ();} return result;} public static void main (string [] ARGs) {system. out. println ("CPU Sn:" + hardwareutils. getcpuserial (); system. out. println ("motherboard Sn:" + hardwareutils. getmotherboardsn (); system. out. println ("C disk Sn:" + hardwareutils. getharddisksn ("C"); system. out. println ("Mac Sn:" + hardwareutils. getmac ());}}
Linux:
Command:
0. View cpuid: dmidecode-T processor | grep 'id'
1. view the server model:Dmidecode | grep 'product name'
2. view the serial number of the motherboard:Dmidecode | grep 'serial number'
3. view the system serial number:Dmidecode-S system-serial-Number
4. view memory information:Dmidecode-T memory
5. View OEM information:Dmidecode-T 11