EthernetHelper.java 1.17 KB
Newer Older
patpat committed
1 2
package com.bgycc.smartcanteen.helper;

3
import java.net.InetAddress;
patpat committed
4
import java.net.NetworkInterface;
5
import java.util.Enumeration;
patpat committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19

public class EthernetHelper {

    public static String getMacAddress() {
        try {
            NetworkInterface networkInterface = NetworkInterface.getByName("eth0");
            byte[] data = networkInterface.getHardwareAddress();
            return String.format("%02x:%02x:%02x:%02x:%02x:%02x", data[0], data[1], data[2], data[3], data[4], data[5]);
        } catch (Exception e) {

        }
        return "02:00:00:00:00:00";
    }

20 21 22 23 24 25 26 27 28 29 30 31 32 33
    public static String getIpString() {
        try {
            NetworkInterface networkInterface = NetworkInterface.getByName("eth0");
            for (Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    String address = inetAddress.getHostAddress();
                    if(!address.contains("::")) return address;
                }
            }
        } catch (Exception ex) {}
        return "0.0.0.0";
    }

patpat committed
34
}