Java to obtain MAC address two methods (recommended) _java

Source: Internet
Author: User

I found a way to get the MAC address on the Internet, and I found two different methods.

First Kind

public static void Main (string[] args) throws Exception {
inetaddress ia = Inetaddress.getlocalhost ();
System.out.println (Getmacaddress (IA));
}
private static String Getmacaddress (InetAddress ia) throws Exception {
//Gets the network interface object (that is, the NIC) and gets the MAC address, which exists in a byte array.
byte[] mac = Networkinterface.getbyinetaddress (IA). gethardwareaddress ();

The following code is to assemble the MAC address into a string
stringbuffer sb = new StringBuffer ();

for (int i = 0; i < mac.length i++) {
if (I!= 0) {
sb.append ("-");
}
Mac[i] & 0xFF is to convert the byte to a positive integer
String s = integer.tohexstring (Mac[i] & 0xFF);
Sb.append (s.length () = = 1 0 + s:s);

Change all lowercase letters of the string to uppercase to become a regular MAC address and return
sb.tostring (). toUpperCase ();

This method looks like it can only take the MAC address of the machine.

Second Kind

public static void Main (string[] args) throws Exception {
Getmac ("192.168.1.186");
public static string Getmac (string IP) {
string str = null;
String mac = null;
try{
Process p = runtime.getruntime (). EXEC ("nbtstat-a" + IP); 
InputStreamReader ir = new InputStreamReader (P.getinputstream (), "GBK"); 
LineNumberReader input = new LineNumberReader (IR); 
for (; true;) { 
str = input.readline (); 
if (str!= null) {
if (Str.indexof ("MAC Address") > 1) {
Mac = str.substring (Str.indexof ("MAC address") + 9);
break; 
}
}} System.out.println (MAC);
catch (IOException e) {
e.printstacktrace ();
}
return mac;
}

I prefer this method, but it may be a little less efficient in time. This has a comparison to note that the data flow there to remember to change to GBK format, otherwise read out of the data is garbled, the back will not be able to, and then identify the field there, there may be some will be "MAC address", so you may need to make some adjustments.

The above is a small series of Java to get the MAC address of the two methods (recommended) All content, I hope that we support cloud Habitat Community ~

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.