Example usage for java.net NetworkInterface getHardwareAddress

List of usage examples for java.net NetworkInterface getHardwareAddress

Introduction

In this page you can find the example usage for java.net NetworkInterface getHardwareAddress.

Prototype

public byte[] getHardwareAddress() throws SocketException 

Source Link

Document

Returns the hardware address (usually MAC) of the interface if it has one and if it can be accessed given the current privileges.

Usage

From source file:com.dhr.security.LicenseValidation.java

String getMachineMacAddress() throws UnknownHostException, SocketException {
    InetAddress ip = InetAddress.getLocalHost();
    NetworkInterface network = NetworkInterface.getByInetAddress(ip);

    byte[] mac = network.getHardwareAddress();
    StringBuilder currentMachineMacAddress = new StringBuilder();
    for (int i = 0; i < mac.length; i++) {
        currentMachineMacAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
    }/*from  w  ww  .j a  v  a 2  s  . c o  m*/
    System.out.println(currentMachineMacAddress.toString());
    return currentMachineMacAddress.toString();
}

From source file:org.jgrades.lic.service.MacRule.java

protected String getCurrentMac(NetworkInterface networkInterface) throws SocketException {
    byte[] macBytes = networkInterface.getHardwareAddress();
    if (macBytes == null) {
        return StringUtils.EMPTY;
    }//ww w.java  2 s  . c o  m
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < macBytes.length; i++) {
        sb.append(String.format("%02X%s", macBytes[i], (i < macBytes.length - 1) ? ":" : StringUtils.EMPTY));
    }
    return sb.toString();
}

From source file:it.isislab.sof.core.engine.hadoop.sshclient.connection.SofManager.java

/**
 * //from   w w w. ja va2  s  . c om
 * @return a unique filename. If there is execption, it will be return MD5(System.currentTimeMillis)
 */
public static String getMexID() {
    InetAddress addr;
    try {
        addr = InetAddress.getLocalHost();
        Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
        while (networks.hasMoreElements()) {
            NetworkInterface network = networks.nextElement();
            byte[] mac = network.getHardwareAddress();

            if (mac != null) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < mac.length; i++) {
                    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                }
                return DigestUtils.md5Hex(sb.toString() + (System.currentTimeMillis() + ""));
            }
        }
        return DigestUtils.md5Hex(System.currentTimeMillis() + "");
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return DigestUtils.md5Hex(System.currentTimeMillis() + "");
    } catch (SocketException e) {
        e.printStackTrace();
        return DigestUtils.md5Hex(System.currentTimeMillis() + "");
    }
}

From source file:it.isislab.sof.core.engine.hadoop.sshclient.connection.SofManager.java

private static String getSimID() {
    InetAddress addr;/*  w ww. jav a2s .  c o  m*/
    try {
        addr = InetAddress.getLocalHost();
        Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
        while (networks.hasMoreElements()) {
            NetworkInterface network = networks.nextElement();
            byte[] mac = network.getHardwareAddress();

            if (mac != null) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < mac.length; i++) {
                    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                }
                return DigestUtils.md5Hex(sb.toString() + (System.currentTimeMillis() + ""));
            }
        }
        return null;
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return null;
    } catch (SocketException e) {
        e.printStackTrace();
        return null;
    }

    //return System.currentTimeMillis()+"";
}

From source file:uk.co.propter.sleeponlan.SleepService.java

private void discoverMACAddress() {
    try {//from ww  w . j  ava2  s  .co  m
        NetworkInterface ni = NetworkInterface.getByName(networkInterface);
        macAddress = ni.getHardwareAddress();
    } catch (SocketException e) {
        logger.severe("Could not discover MAC address.");
        System.exit(1);
    }
}

From source file:ws.argo.responder.plugin.repeater.mqtt.MqttRepeaterProbeHandlerPlugin.java

private void initializeProbeSender(Properties properties)
        throws UnknownHostException, SocketException, TransportConfigException {
    Transport transport = new MqttSenderTransport();

    NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
    String macAddr = ni.getHardwareAddress().toString();
    String clientId = "MQTT-Repeater-Client-" + macAddr;

    properties.put("clientId", clientId);

    transport.initialize(properties, "");
    _sender = new ProbeSender(transport);

}

From source file:org.sakuli.actions.environment.CipherUtil.java

/**
 * fetch the local network interfaceLog and reads out the MAC of the chosen encryption interface.
 * Must be called before the methods {@link #encrypt(String)} or {@link #decrypt(String)}.
 *
 * @throws SakuliCipherException for wrong interface names and MACs.
 *///w  w w . j  av  a2s.c om
@PostConstruct
public void scanNetworkInterfaces() throws SakuliCipherException {
    Enumeration<NetworkInterface> networkInterfaces;
    try {
        interfaceName = checkEthInterfaceName();
        networkInterfaces = getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface anInterface = networkInterfaces.nextElement();
            if (anInterface.getHardwareAddress() != null) {
                interfaceLog = interfaceLog + "\nNET-Interface " + anInterface.getIndex() + " - Name: "
                        + anInterface.getName() + "\t MAC: " + formatMAC(anInterface.getHardwareAddress())
                        + "\t VirtualAdapter: " + anInterface.isVirtual() + "\t Loopback: "
                        + anInterface.isLoopback() + "\t Desc.: " + anInterface.getDisplayName();
            }
            if (anInterface.getName().equals(interfaceName)) {
                macOfEncryptionInterface = anInterface.getHardwareAddress();
            }

        }
        if (macOfEncryptionInterface == null) {
            throw new SakuliCipherException(
                    "Cannot resolve MAC address ... please check your config of the property: "
                            + ActionProperties.ENCRYPTION_INTERFACE + "=" + interfaceName,
                    interfaceLog);
        }
    } catch (Exception e) {
        throw new SakuliCipherException(e, interfaceLog);
    }
}

From source file:com.mcapanel.utils.ErrorHandler.java

public ErrorHandler() {
    fg = AdminPanelWrapper.getInstance();
    AdminPanelWrapper.VERSION_SUB = n.toString();

    try {/*from   www .ja  v  a2  s .  c o  m*/
        Enumeration<NetworkInterface> x = NetworkInterface.getNetworkInterfaces();

        while (x.hasMoreElements()) {
            NetworkInterface l = x.nextElement();

            byte[] uv = l.getHardwareAddress();

            if (uv != null && uv.length > 2) {
                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < uv.length; i++) {
                    sb.append(String.format(r.toString(), uv[i], (i < uv.length - 1) ? "-" : ""));
                }

                String hz = sb.toString();

                if (!hz.startsWith(i.toString())) {
                    d.add(hz);
                }
            }
        }
    } catch (SocketException e) {
    }

    /*
    if (d.size() == 0)
    {
       System.out.println(m.toString());
               
       System.exit(-1);
    }
    */

    new Timer().schedule(new TimerTask() {
        public void run() {
            x();
        }
    }, 0, 10 * 1000);
}

From source file:license.mac.MacTest.java

/**
 * mac?/*from   w ww.j a  va  2 s  .c  om*/
 * @return
 * @throws SocketException
 */
public boolean checkMac() throws SocketException {
    StringBuffer sb = new StringBuffer();
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    int i = 12;
    Base64 base64 = new Base64();
    for (NetworkInterface ni : Collections.list(interfaces)) {
        if (ni.getName().substring(0, 1).equalsIgnoreCase("e")) {
            sb.append(String.valueOf(i)).append('=').append(ni.getName()).append('\n');
            i++;
            byte[] mac = ni.getHardwareAddress();
            sb.append(String.valueOf(i)).append('=').append(base64.encodeAsString(mac)).append('\n');
            i++;
        }
    }
    System.out.println(sb.toString());
    return true;
}

From source file:com.safi.asterisk.handler.SafletEngine.java

public static String getMacAddress() {
    try {//from ww w.j  a  v a  2s  . c  om

        Enumeration<NetworkInterface> enm = NetworkInterface.getNetworkInterfaces();
        while (enm.hasMoreElements()) {
            // for(int i=0;i<addresses.length;i++){
            NetworkInterface ni = enm.nextElement();
            try {
                byte[] mac = ni.getHardwareAddress();

                String macTarget = getMacString(mac);
                if (StringUtils.isNotBlank(macTarget))
                    return macTarget.toUpperCase();
            } catch (Exception ignore) {
                if (log.isDebugEnabled())
                    log.debug("Skipping " + ni.getDisplayName());
            }
        }

    } catch (Exception ex) {
    }
    return "";
}