Java get CPU serial number and Nic MAC address __java

Source: Internet
Author: User
Tags readline string find
Turn from: http://dev2dev.bea.com.cn/bbs/thread.jspa?forumID=124&threadID=28320&messageID=171767
Java obtains the CPU serial number and the network card MAC address
/*
Using the commands of the runtime call operating system, the specific commands depend on the different operating systems, and do not invoke the Runtime.getruntime (). EXEC (String) interface to use Runtime.getruntime (). EXEC ( String[]) This interface, or the execution of complex commands can be problematic. Examples are as follows (take the number of CPUs, other similar):
To define a command:
Windowscmd = "cmd.exe/c echo%number_of_processors%";//windows Special
Solariscmd = {"/bin/sh", "-C", "/usr/sbin/psrinfo | Wc-l "};
Aixcmd = {"/bin/sh", "-C", "/USR/SBIN/LSDEV-CC processor | Wc-l "};
Hpuxcmd = {"/bin/sh", "-C", "Echo/" map/"|/usr/sbin/cstm | grep CPU | wc-l"};
Linuxcmd = {"/bin/sh", "-C", "Cat/proc/cpuinfo | grep ^process | Wc-l "};

Then judge the system:
OS = System.getproperty ("Os.name"). toLowerCase ();

Call different commands depending on the operating system.
*/
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.LineNumberReader;

public class Getmacaddress
{
public string getmacaddress (string ipaddress)
{
String str = "", Strmac = "", macAddress = "";
Try
{
Process pp = Runtime.getruntime (). EXEC ("nbtstat-a" + ipaddress);
InputStreamReader ir = new InputStreamReader (Pp.getinputstream ());
LineNumberReader input = new LineNumberReader (IR);
for (int i = 1;i < 100;i++)
{
str = Input.readline ();
if (str!= null)
{
if (Str.indexof ("MAC address") > 1)
{
Strmac = str.substring (Str.indexof ("MAC address") + 14,str.length ());
Break
}
}
}
}
catch (IOException ex)
{
return "Can ' t get MAC address!";
}
//
if (Strmac.length () < 17)
{
return "error!";
}
macAddress = strmac.substring (0,2) + ":"
+ strmac.substring (3,5) + ":"
+ strmac.substring (6,8) + ":"
+ strmac.substring (9,11) + ":"
+ strmac.substring (12,14) + ":"
+ strmac.substring (15,17);
//
return macAddress;
}

public static void Main (string[] args)
{
Getmacaddress getmacaddress = new getmacaddress ();
System.out.println (getmacaddress.getmacaddress ("172.18.8.225"));

Try
{
java.lang.Process proc = Runtime.getruntime (). EXEC ("Ipconfig/all");
InputStream ISTR = Proc.getinputstream ();
byte[] data = new byte[1024];
Istr.read (data);
String netdata = new string (data);
System.out.println ("Your Mac address=" + procall (netdata));
}
catch (IOException E)
{
System.out.println ("error=" + e);
}
}

public static string Procall (String str)
{
Return Procstringend (Procfirstmac (procaddress (str)));
}

public static string procaddress (String str)
{
int indexof = Str.indexof ("Physical Address");
if (indexof > 0)
{
Return str.substring (Indexof,str.length ());
}
return str;
}

public static string Procfirstmac (String str)
{
int indexof = Str.indexof (":");
if (indexof > 0)
{
Return str.substring (indexof + 1,str.length ()). Trim ();
}
return str;
}

public static string Procstringend (String str)
{
int indexof = Str.indexof ("/R");
if (indexof > 0)
{
Return str.substring (0,indexof). Trim ();
}
return str;
}
}



Import Java.util.Vector;

Class Getnetmac
{
Network Card Physical Address length
Static private final int _physicallength = 16;

public static void Main (string[] args)
{
Output Computer Phycail IP Address
System.out.println ("The MAC addressis:/t" + getphysicaladdress ());
}

static public String getphysicaladdress ()
{
Getnetmacshell shell = new Getnetmacshell ();
String cmd = "cmd.exe/c ipconfig/all";
Vector result;
result = Shell.execute (cmd);
Return Parsecmd (Result.tostring ());
}

Parse out the desired string from the string
Static private String Parsecmd (string s)
{
String find = "Physical address ...:";
int findindex = S.indexof (find);
if (FindIndex = = 1)
{
Return "Don't find";
}
Else
{
Return s.substring (FindIndex + find.length () + 1,findindex + find.length () + 1 + _physicallength);
}
}
}




--------------------------------------------------------------------------------
Read a bit and take it out, then come back read some more.



http://202.101.111.1/123/
Http://iAMin.BlogDriver.com


Yulimin [Moderator]

Number of Posts: 1,577
Active Score: 1,591
Technology Score: 196
Available Currency: 486
Registration time: 2002-9-4
User status: Normal

Re:java get CPU serial number and Nic MAC address

Submission Time: Nov 2, 11:51:28 AM Reference reply Send Message


Import Java.io.BufferedReader;
Import Java.io.DataInputStream;
Import Java.io.InputStreamReader;
Import Java.util.Vector;

public class Getnetmacshell
{
Private process process = NULL;

Public Vector Execute (String shellcommand)
{
Try
{
Start (Shellcommand);
Vector vresult = new vector ();
DataInputStream in = new DataInputStream (Process.getinputstream ());
BufferedReader reader = new BufferedReader (new InputStreamReader (in));

String Line;
Todo
{
line = Reader.readline ();
if (line = = null)
{
Break
}
Else
{
Vresult.addelement (line);
}
}
while (true);
Reader.close ();
return vresult;

}
catch (Exception e)
{
Error
return null;
}
}

public void Start (String shellcommand)
{
Try
{
if (process!= null)
{
Kill ();
}
Runtime sys = runtime.getruntime ();
Process = Sys.exec (Shellcommand);
}
catch (Exception e)
{
System.out.println (E.tostring ());
}
}

public void Kill ()
{
if (process!= null)
{
Process.destroy ();
Process = NULL;
}
}
}
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.