| .. | .. |
|---|
| 1 | 1 | package net.curisit.securis.utils; |
|---|
| 2 | 2 | |
|---|
| 3 | | -import java.net.InetAddress; |
|---|
| 3 | +import java.io.InputStream; |
|---|
| 4 | 4 | import java.net.NetworkInterface; |
|---|
| 5 | 5 | import java.net.SocketException; |
|---|
| 6 | | -import java.net.UnknownHostException; |
|---|
| 7 | 6 | import java.util.ArrayList; |
|---|
| 8 | 7 | import java.util.Collections; |
|---|
| 9 | 8 | import java.util.List; |
|---|
| 9 | +import java.util.regex.Matcher; |
|---|
| 10 | +import java.util.regex.Pattern; |
|---|
| 10 | 11 | |
|---|
| 11 | 12 | import net.curisit.securis.SeCurisException; |
|---|
| 12 | 13 | |
|---|
| 14 | +import org.apache.commons.io.IOUtils; |
|---|
| 15 | +import org.apache.commons.lang3.StringUtils; |
|---|
| 13 | 16 | import org.apache.logging.log4j.LogManager; |
|---|
| 14 | 17 | import org.apache.logging.log4j.Logger; |
|---|
| 15 | 18 | |
|---|
| .. | .. |
|---|
| 21 | 24 | */ |
|---|
| 22 | 25 | public class HWInfo { |
|---|
| 23 | 26 | |
|---|
| 24 | | - private static final Logger LOG = LogManager.getLogger(HWInfo.class); |
|---|
| 27 | + private static final Logger log = LogManager.getLogger(HWInfo.class); |
|---|
| 25 | 28 | |
|---|
| 26 | 29 | public static String getOsName() { |
|---|
| 27 | 30 | return System.getProperty("os.name"); |
|---|
| .. | .. |
|---|
| 36 | 39 | } |
|---|
| 37 | 40 | |
|---|
| 38 | 41 | /** |
|---|
| 39 | | - * Get MAC address |
|---|
| 42 | + * Gets MAC address using Java. Java does not show information about down interfaces |
|---|
| 40 | 43 | * |
|---|
| 41 | 44 | * @return |
|---|
| 42 | 45 | */ |
|---|
| 43 | 46 | public static List<String> getMACAddress() throws SeCurisException { |
|---|
| 47 | + log.info("Retrieving HW info with Java VM"); |
|---|
| 44 | 48 | List<byte[]> macs = new ArrayList<byte[]>(); |
|---|
| 45 | 49 | try { |
|---|
| 46 | | - // Get MAC for ethX interface, where X is the lower number with MAC address != null |
|---|
| 47 | 50 | for (NetworkInterface network : Collections.list(NetworkInterface.getNetworkInterfaces())) { |
|---|
| 48 | 51 | if (!network.isLoopback() && !network.isVirtual() && !network.isPointToPoint() && network.getHardwareAddress() != null) { |
|---|
| 49 | 52 | macs.add(network.getHardwareAddress()); |
|---|
| 50 | | - LOG.debug("Interface added {}, MAC: {}", network.getName(), network.getHardwareAddress()); |
|---|
| 53 | + log.debug("Interface added {}, MAC: {}", network.getName(), network.getHardwareAddress()); |
|---|
| 51 | 54 | logInterface(network); |
|---|
| 52 | 55 | } |
|---|
| 53 | 56 | } |
|---|
| 54 | 57 | |
|---|
| 55 | | - // If not found interface ethX |
|---|
| 56 | 58 | if (macs.isEmpty()) { |
|---|
| 57 | | - NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()); |
|---|
| 58 | | - if (network.getHardwareAddress() != null) |
|---|
| 59 | | - macs.add(network.getHardwareAddress()); |
|---|
| 60 | | - LOG.debug("Selected interface (Inet Address rule)"); |
|---|
| 61 | | - logInterface(network); |
|---|
| 62 | | - } |
|---|
| 63 | | - |
|---|
| 64 | | - // If not found interface, last attempt, we get any mac |
|---|
| 65 | | - if (macs.isEmpty()) { |
|---|
| 66 | | - for (NetworkInterface network : Collections.list(NetworkInterface.getNetworkInterfaces())) { |
|---|
| 67 | | - if (!network.isLoopback() && !network.isVirtual() && !network.isPointToPoint() && network.getHardwareAddress() != null) { |
|---|
| 68 | | - if (network.getHardwareAddress() != null) |
|---|
| 69 | | - macs.add(network.getHardwareAddress()); |
|---|
| 70 | | - LOG.debug("Selected interface (Any with MAC rule)"); |
|---|
| 71 | | - logInterface(network); |
|---|
| 72 | | - break; |
|---|
| 73 | | - } |
|---|
| 74 | | - } |
|---|
| 75 | | - } |
|---|
| 76 | | - |
|---|
| 77 | | - if (macs.isEmpty()) |
|---|
| 78 | 59 | throw new SeCurisException("Unable to get MAC address"); |
|---|
| 60 | + } |
|---|
| 79 | 61 | |
|---|
| 80 | 62 | List<String> macAddresses = new ArrayList<String>(); |
|---|
| 81 | 63 | for (byte[] mac : macs) { |
|---|
| 82 | 64 | macAddresses.add(printMacAddress(mac)); |
|---|
| 83 | 65 | } |
|---|
| 84 | | - LOG.debug("MAC Addresses: {}", macAddresses); |
|---|
| 66 | + log.info("MAC Addresses: {}", macAddresses); |
|---|
| 85 | 67 | return macAddresses; |
|---|
| 86 | 68 | |
|---|
| 87 | | - } catch (UnknownHostException e) { |
|---|
| 88 | | - throw new SeCurisException("Unable to get MAC address", e); |
|---|
| 89 | | - } catch (SocketException e) { |
|---|
| 90 | | - throw new SeCurisException("Unable to get MAC address", e); |
|---|
| 91 | 69 | } catch (Exception e) { |
|---|
| 92 | 70 | throw new SeCurisException("Unable to get MAC address", e); |
|---|
| 71 | + } |
|---|
| 72 | + } |
|---|
| 73 | + |
|---|
| 74 | + /** |
|---|
| 75 | + * Gets MAC address natively using ipconfig or ifconfig. |
|---|
| 76 | + * @return |
|---|
| 77 | + * @throws CurisException |
|---|
| 78 | + */ |
|---|
| 79 | + public static List<String> getMACAddressNatively() throws SeCurisException { |
|---|
| 80 | + try { |
|---|
| 81 | + log.info("Retrieving HW info natively"); |
|---|
| 82 | + String output; |
|---|
| 83 | + if (isWindows()) { |
|---|
| 84 | + output = executeCommand(new String[] {"ipconfig", "/all"}); |
|---|
| 85 | + } else { |
|---|
| 86 | + output = executeCommand(new String[] {"ifconfig", "-a"}); |
|---|
| 87 | + } |
|---|
| 88 | + //String output = FileUtils.readFileToString(new File("/Users/cesar/Downloads/ipconfig_no_mac.txt"), "UTF-8"); |
|---|
| 89 | + log.debug("Command output {}", output); |
|---|
| 90 | + List<String> macs = extractMacs(output); |
|---|
| 91 | + log.debug("Macs found: {}", macs); |
|---|
| 92 | + return macs; |
|---|
| 93 | + } catch (SeCurisException ce) { |
|---|
| 94 | + throw ce; |
|---|
| 93 | 95 | } |
|---|
| 94 | 96 | } |
|---|
| 97 | + |
|---|
| 98 | + /** |
|---|
| 99 | + * Tries to retrieve MAC address natively, if it fails then it tries to retrieve it using Java |
|---|
| 100 | + * @return |
|---|
| 101 | + * @throws CurisException |
|---|
| 102 | + */ |
|---|
| 103 | + public static List<String> getMACAddressNativelyFailback() throws SeCurisException { |
|---|
| 104 | + try { |
|---|
| 105 | + return getMACAddressNatively(); |
|---|
| 106 | + } catch (SeCurisException e) { |
|---|
| 107 | + log.info("Error getting HW info natively" .concat(e.getMessage())); |
|---|
| 108 | + return getMACAddress(); |
|---|
| 109 | + } |
|---|
| 110 | + } |
|---|
| 111 | + |
|---|
| 112 | + /** |
|---|
| 113 | + * Finds MAC in a string. Match string like this: |
|---|
| 114 | + * Unix: 60:03:08:95:ae:d0 |
|---|
| 115 | + * Windows: 0A-00-27-00-00-00 |
|---|
| 116 | + * |
|---|
| 117 | + * @param line |
|---|
| 118 | + * @return |
|---|
| 119 | + * @throws CurisException |
|---|
| 120 | + */ |
|---|
| 121 | + private static List<String> extractMacs(String line) throws SeCurisException { |
|---|
| 122 | + Pattern pattern = Pattern.compile("(?m)( )([0-9a-fA-F][0-9a-fA-F][:-]){5}([0-9a-fA-F][0-9a-fA-F])($| )"); |
|---|
| 123 | + |
|---|
| 124 | + |
|---|
| 125 | + List<String> macs = new ArrayList<>(); |
|---|
| 126 | + Matcher matcher = pattern.matcher(line); |
|---|
| 127 | + while(matcher.find()){ |
|---|
| 128 | + String mac = matcher.group().trim().replaceAll(":", "-").toUpperCase(); |
|---|
| 129 | + if (!mac.equals("00-00-00-00-00-00")) { |
|---|
| 130 | + macs.add(mac); |
|---|
| 131 | + } |
|---|
| 132 | + } |
|---|
| 133 | + |
|---|
| 134 | + if (macs.isEmpty()) { |
|---|
| 135 | + throw new SeCurisException("Mac is not found"); |
|---|
| 136 | + } |
|---|
| 137 | + |
|---|
| 138 | + return macs; |
|---|
| 139 | + } |
|---|
| 140 | + |
|---|
| 141 | + /** |
|---|
| 142 | + * Executes the given command and return the standard output. |
|---|
| 143 | + * @param command |
|---|
| 144 | + * @return |
|---|
| 145 | + * @throws Exception |
|---|
| 146 | + */ |
|---|
| 147 | + private static String executeCommand(String[] command) throws SeCurisException { |
|---|
| 148 | + try { |
|---|
| 149 | + Process process = new ProcessBuilder(command).start(); |
|---|
| 150 | + InputStream is = process.getInputStream(); |
|---|
| 151 | + |
|---|
| 152 | + String output = IOUtils.toString(is, "UTF-8"); |
|---|
| 153 | + |
|---|
| 154 | + if (process == null || process.exitValue() != 0) { |
|---|
| 155 | + throw new SeCurisException(String.format("Error executing command %s", StringUtils.join(command, " "))); |
|---|
| 156 | + } |
|---|
| 157 | + return output; |
|---|
| 158 | + } catch (Exception e) { |
|---|
| 159 | + throw new SeCurisException(String.format("Error executing command %s", StringUtils.join(command, " ")), e); |
|---|
| 160 | + } |
|---|
| 161 | + } |
|---|
| 162 | + |
|---|
| 163 | + /** |
|---|
| 164 | + * Returns true is OOSS is windows |
|---|
| 165 | + * @return |
|---|
| 166 | + */ |
|---|
| 167 | + public static boolean isWindows() { |
|---|
| 168 | + String os = System.getProperty("os.name").toLowerCase(); |
|---|
| 169 | + return (os.indexOf("win") >= 0); |
|---|
| 170 | + } |
|---|
| 95 | 171 | |
|---|
| 96 | 172 | /** |
|---|
| 97 | 173 | * Get microprocessor name |
|---|
| .. | .. |
|---|
| 103 | 179 | } |
|---|
| 104 | 180 | |
|---|
| 105 | 181 | private static void logInterface(NetworkInterface network) { |
|---|
| 106 | | - LOG.debug("Interface name: {}", network.getName()); |
|---|
| 107 | | - LOG.debug("Interface display name: {}", network.getDisplayName()); |
|---|
| 182 | + log.debug("Interface name: {}", network.getName()); |
|---|
| 183 | + log.debug("Interface display name: {}", network.getDisplayName()); |
|---|
| 108 | 184 | try { |
|---|
| 109 | | - LOG.debug("Interface mac: {}", printMacAddress(network.getHardwareAddress())); |
|---|
| 185 | + log.debug("Interface mac: {}", printMacAddress(network.getHardwareAddress())); |
|---|
| 110 | 186 | } catch (SocketException e) { |
|---|
| 111 | 187 | // Silent |
|---|
| 112 | 188 | } |
|---|