/** * Extract the MAC address of an IP and write it in the following format: 12-34-AA-BB-CC-DD * @param ip IP address - ignored if <code>null</code>/empty * @return The formatted MAC address - <code>null</code> if unable to extract * it - e.g., {@link NetworkInterface} <code>getHardwareAddress</code> was * was introduced in JDK 1.6, so if we are running on a JVM below that * version, then we cannot extract the MAC address (though this is <U>not</U> * the only reason why a MAC might not be available) */ public static String getMac(String ip) { // since NetworkInterface.getHardwareAddress was introduced in JDK 1.6, we need to call it by reflection if ((macAddressMethod == null) || StringUtil.isEmpty(ip)) { return null; } try { NetworkInterface nif=NetworkInterface.getByInetAddress(InetAddress.getByName(ip)); byte[] mac=getMac(nif); return getMac(mac); } catch (Exception ex) { return null; } }
public Map<String, String> getProperties(Map<String, String> existingProperties) { Map<String, String> props = new HashMap<String, String>(); String preferredIp = getPreferredIp(existingProperties); props.put(getMappedPropertyKey(NETWORK_IP_KEY), preferredIp); String macAddress = NetworkAddressUtil.getMac(preferredIp); if (!StringUtil.isEmpty(macAddress)) { props.put(getMappedPropertyKey(NETWORK_MAC_KEY), macAddress); } return props; }