JAVA supports cross-platform access to cpuid, motherboard ID, hard disk ID, and MAC address (compatible with Windows and Linux)

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.