package net.curisit.securis; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Retrieve HW info * * @author cesarcalvo * */ public class HWInfo { private static final Logger log = LoggerFactory.getLogger(HWInfo.class); public static String getOsName() { return System.getProperty("os.name"); } public static String getArch() { return System.getProperty("os.arch"); } public static int getNumCpus() { return Runtime.getRuntime().availableProcessors(); } /** * Get MAC address * * @return */ public static List getMACAddress() throws SeCurisException { List macs = new ArrayList(); try { // Get MAC for ethX interface, where X is the lower number with MAC address != null for (NetworkInterface network : Collections.list(NetworkInterface.getNetworkInterfaces())) { if (!network.isLoopback() && !network.isVirtual() && !network.isPointToPoint() && network.getHardwareAddress() != null) { macs.add(network.getHardwareAddress()); log.debug("Interface added {}, MAC: {}", network.getName(), network.getHardwareAddress()); logInterface(network); } } // If not found interface ethX if (macs.isEmpty()) { NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()); if (network.getHardwareAddress() != null) macs.add(network.getHardwareAddress()); log.debug("Selected interface (Inet Address rule)"); logInterface(network); } // If not found interface, last attempt, we get any mac if (macs.isEmpty()) { for (NetworkInterface network : Collections.list(NetworkInterface.getNetworkInterfaces())) { if (!network.isLoopback() && !network.isVirtual() && !network.isPointToPoint() && network.getHardwareAddress() != null) { if (network.getHardwareAddress() != null) macs.add(network.getHardwareAddress()); log.debug("Selected interface (Any with MAC rule)"); logInterface(network); break; } } } if (macs.isEmpty()) throw new SeCurisException("Unable to get MAC address"); List macAddresses = new ArrayList(); for (byte[] mac : macs) { macAddresses.add(printMacAddress(mac)); } log.info("MAC Addresses: {}", macAddresses); return macAddresses; } catch (UnknownHostException e) { throw new SeCurisException("Unable to get MAC address", e); } catch (SocketException e) { throw new SeCurisException("Unable to get MAC address", e); } catch (Exception e) { throw new SeCurisException("Unable to get MAC address", e); } } /** * Get microprocessor name * * @return */ public static String getCPUName() throws SeCurisException { return System.getenv("PROCESSOR_IDENTIFIER"); } private static void logInterface(NetworkInterface network) { log.debug("Interface name: {}", network.getName()); log.debug("Interface display name: {}", network.getDisplayName()); try { log.debug("Interface mac: {}", printMacAddress(network.getHardwareAddress())); } catch (SocketException e) { // Silent } } private static String printMacAddress(byte[] mac) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { sb.append(String.format("%s%02X", (i > 0) ? "-" : "", mac[i])); } return sb.toString(); } }