How to use NetworkInterface to get server MAC address _java

Source: Internet
Author: User
Tags stringbuffer

In many cases, we need to obtain server hardware information (such as MAC address), commonly used in several ways:
• Use a command-line program to get the hard drive information, and then get the output stream via Runtime.getruntime (). exec, then get the MAC address via string analysis
• Compile the local program and then invoke it through JNI

Both of these methods need to distinguish between different operating system platform, coding, more trouble, such as
Windows platform needs to use Iptables/all command
Linux platform needs to use Ifconfig command

Today introduces a general-purpose cross-platform operation, that is, JDK's NetworkInterface interface, the interface in the JDK1.4 has appeared, but the function is relatively few, JDK1.6 added a lot of new features after, relatively good.

Specific features you can refer to the API documentation, here is mainly about how to obtain the server MAC address, the code is as follows, there are comments, not much to say.

Copy Code code as follows:

Get the MAC address of all network adapters
public static list<string> Getallmac () {
list<string> list = new arraylist<string> ();
try {
Enumeration<networkinterface> e = networkinterface.getnetworkinterfaces ();//Returns an enumerated instance of all network interfaces
while (E.hasmoreelements ()) {
NetworkInterface network = e.nextelement ();//Get Current network interface
if (network!= null) {
if (network.gethardwareaddress ()!= null) {
Get MAC Address
The result is a byte array, each of which is a byte, and we need to convert the Parsebyte method into a common hexadecimal representation
byte[] addres = network.gethardwareaddress ();
StringBuffer sb = new StringBuffer ();
if (addres!= null && addres.length > 1) {
Sb.append (Parsebyte (addres[0)). Append (":"). Append (
Parsebyte (Addres[1]). Append (":"). Append (
Parsebyte (Addres[2]). Append (":"). Append (
Parsebyte (Addres[3]). Append (":"). Append (
Parsebyte (Addres[4]). Append (":"). Append (
Parsebyte (addres[5]));
List.add (Sb.tostring ());
}
}
} else {
System.out.println ("Get MAC address occurrence exception");
}
}
catch (SocketException e) {
E.printstacktrace ();
}
return list;
}
Format binary
private static String Parsebyte (Byte b) {
int intvalue = 0;
if (b >= 0) {
Intvalue = b;
} else {
Intvalue = 256 + b;
}
Return integer.tohexstring (Intvalue);
}

We then use the following test code to look at the test results
Copy Code code as follows:

list<string> list = Getallmac ();
for (String mac:list) {
System.out.println (MAC);
}

The output results are as follows:
Copy Code code as follows:

0:18:8b:cc:xx:e3
0:0:0:0:0:0:0:e0
0:50:xx:c0:0:1
0:50:xx:c0:0:8

We found that "0:18:8b:cc:xx:e3", only a 0, looks very awkward ah, we may wish to revise the Parsebyte method, as follows:
Copy Code code as follows:

private static String Parsebyte (Byte b) {
String s = "+integer.tohexstring" (BYTE0);
Return s.substring (S.length ()-2);
}

The output is changed:
Copy Code code as follows:

00:18:8b:cc:xx:e3
00:00:00:00:00:e0
00:50:xx:c0:00:01
00:50:xx:c0:00:08

It looks a lot more comfortable, doesn't it?
In addition, the NetworkInterface interface also provides the following methods, you can refer to.

string DisplayName () get the display name of the network interface
int Getmtu () returns the maximum transmission unit for this interface (Maximum transmission UNIT,MTU)
string GetName () Gets the name of this network interface
boolean Isloopback () returns whether this network interface is a loopback interface
boolean Ispointtopoint () returns whether this network interface is a point-to-point interface
boolean isUp () returns whether this network interface is open and running
boolean isvirtual () returns whether this interface is a virtual interface

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.