📌  相关文章
📜  获取Windows和Linux机器系统MAC地址的Java程序

📅  最后修改于: 2022-05-13 01:55:15.496000             🧑  作者: Mango

获取Windows和Linux机器系统MAC地址的Java程序

媒体访问控制地址(MAC 地址)是分配给网络接口控制器 (NIC)的唯一十六进制标识符,用作网段内通信中的网络地址。这种用法在大多数IEEE 802网络技术中很常见,包括以太网、Wi-Fi 和蓝牙。在开放系统互连 (OSI)网络模型中,MAC 地址用于数据链路层的媒体访问控制协议子层。正如通常表示的那样,MAC 地址可识别为六组两个十六进制数字,由连字符、冒号或不带分隔符分隔。

MAC 地址主要由设备制造商分配,因此经常被称为烧入地址,或以太网硬件地址、硬件地址或物理地址。

mac 或物理地址

示例 1

Java
// Java program to access the MAC address of the
// localhost machine
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class MACAddress {
  
    // method to get the MAC Address
    void getMAC(InetAddress addr) throws SocketException
    {
        // create a variable of type NetworkInterface and
        // assign it with the value returned by the
        // getByInetAddress() method
        NetworkInterface iface
            = NetworkInterface.getByInetAddress(addr);
  
        // create a byte array and store the value returned
        // by the NetworkInterface.getHardwareAddress()
        // method
        byte[] mac = iface.getHardwareAddress();
        
        // convert the obtained byte array into a printable
        // String
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format(
                "%02X%s", mac[i],
                (i < mac.length - 1) ? "-" : ""));
        }
        
        // print the final String containing the MAC Address
        System.out.println(sb.toString());
    }
  
    // Driver method
    public static void main(String[] args) throws Exception
    {
        // a variable of type InetAddress to store the
        // address of the local host
        InetAddress addr = InetAddress.getLocalHost();
        
        // instantiate the MACAddress class
        MACAddress obj = new MACAddress();
        System.out.print("MAC Address of the system : ");
        
        // call the getMAC() method on the current object
        // passing the localhost address as the parameter
        obj.getMAC(addr);
    }
}


Java
// Java program to access all the MAC addresses of the
// localhost machine
  
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class MACAddress {
    public static void main(String[] args) throws Exception
    {
        // instantiate the MACAddress class
        MACAddress obj = new MACAddress();
        
        // call the getMAC() method on the current object
        // passing the localhost address as the parameter
        obj.getMAC();
    }
  
    // method to get the MAC addresses of the
    // localhost machine
    void getMAC()
    {
        try {
            
            // create an Enumeration of type
            // NetworkInterface and store the values
            // returned by
            // NetworkInterface.getNetworkInterfaces()
            // method
            Enumeration networks
                = NetworkInterface.getNetworkInterfaces();
            
            // for every network in the networks Enumeration
            while (networks.hasMoreElements()) {
                NetworkInterface network
                    = networks.nextElement();
                
                // call getHardwareAddress() method on each
                // network and store the returned value in a
                // byte array
                byte[] mac = network.getHardwareAddress();
  
                if (mac != null) {
                    System.out.print(
                        "Current MAC address : ");
                    
                    // convert the obtained byte array into
                    // a printable String
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                        sb.append(String.format(
                            "%02X%s", mac[i],
                            (i < mac.length - 1) ? "-"
                                                 : ""));
                    }
                    
                    // print the final String containing the
                    // MAC Address
                    System.out.println(sb.toString());
                }
            }
        }
        catch (SocketException e) {
            e.printStackTrace();
        }
    }
}


输出

物理或mac地址

例2(当设备有多个MAC地址时

Java

// Java program to access all the MAC addresses of the
// localhost machine
  
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class MACAddress {
    public static void main(String[] args) throws Exception
    {
        // instantiate the MACAddress class
        MACAddress obj = new MACAddress();
        
        // call the getMAC() method on the current object
        // passing the localhost address as the parameter
        obj.getMAC();
    }
  
    // method to get the MAC addresses of the
    // localhost machine
    void getMAC()
    {
        try {
            
            // create an Enumeration of type
            // NetworkInterface and store the values
            // returned by
            // NetworkInterface.getNetworkInterfaces()
            // method
            Enumeration networks
                = NetworkInterface.getNetworkInterfaces();
            
            // for every network in the networks Enumeration
            while (networks.hasMoreElements()) {
                NetworkInterface network
                    = networks.nextElement();
                
                // call getHardwareAddress() method on each
                // network and store the returned value in a
                // byte array
                byte[] mac = network.getHardwareAddress();
  
                if (mac != null) {
                    System.out.print(
                        "Current MAC address : ");
                    
                    // convert the obtained byte array into
                    // a printable String
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                        sb.append(String.format(
                            "%02X%s", mac[i],
                            (i < mac.length - 1) ? "-"
                                                 : ""));
                    }
                    
                    // print the final String containing the
                    // MAC Address
                    System.out.println(sb.toString());
                }
            }
        }
        catch (SocketException e) {
            e.printStackTrace();
        }
    }
}

输出

多个mac地址