César Calvo
2016-09-28 e1d59acfb21c99b8b1b0ca0504b599f9ac444d19
src/main/java/net/curisit/securis/utils/HWInfo.java
....@@ -1,15 +1,18 @@
11 package net.curisit.securis.utils;
22
3
-import java.net.InetAddress;
3
+import java.io.InputStream;
44 import java.net.NetworkInterface;
55 import java.net.SocketException;
6
-import java.net.UnknownHostException;
76 import java.util.ArrayList;
87 import java.util.Collections;
98 import java.util.List;
9
+import java.util.regex.Matcher;
10
+import java.util.regex.Pattern;
1011
1112 import net.curisit.securis.SeCurisException;
1213
14
+import org.apache.commons.io.IOUtils;
15
+import org.apache.commons.lang3.StringUtils;
1316 import org.apache.logging.log4j.LogManager;
1417 import org.apache.logging.log4j.Logger;
1518
....@@ -21,7 +24,7 @@
2124 */
2225 public class HWInfo {
2326
24
- private static final Logger LOG = LogManager.getLogger(HWInfo.class);
27
+ private static final Logger log = LogManager.getLogger(HWInfo.class);
2528
2629 public static String getOsName() {
2730 return System.getProperty("os.name");
....@@ -36,62 +39,135 @@
3639 }
3740
3841 /**
39
- * Get MAC address
42
+ * Gets MAC address using Java. Java does not show information about down interfaces
4043 *
4144 * @return
4245 */
4346 public static List<String> getMACAddress() throws SeCurisException {
47
+ log.info("Retrieving HW info with Java VM");
4448 List<byte[]> macs = new ArrayList<byte[]>();
4549 try {
46
- // Get MAC for ethX interface, where X is the lower number with MAC address != null
4750 for (NetworkInterface network : Collections.list(NetworkInterface.getNetworkInterfaces())) {
4851 if (!network.isLoopback() && !network.isVirtual() && !network.isPointToPoint() && network.getHardwareAddress() != null) {
4952 macs.add(network.getHardwareAddress());
50
- LOG.debug("Interface added {}, MAC: {}", network.getName(), network.getHardwareAddress());
53
+ log.debug("Interface added {}, MAC: {}", network.getName(), network.getHardwareAddress());
5154 logInterface(network);
5255 }
5356 }
5457
55
- // If not found interface ethX
5658 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())
7859 throw new SeCurisException("Unable to get MAC address");
60
+ }
7961
8062 List<String> macAddresses = new ArrayList<String>();
8163 for (byte[] mac : macs) {
8264 macAddresses.add(printMacAddress(mac));
8365 }
84
- LOG.debug("MAC Addresses: {}", macAddresses);
66
+ log.info("MAC Addresses: {}", macAddresses);
8567 return macAddresses;
8668
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);
9169 } catch (Exception e) {
9270 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;
9395 }
9496 }
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
+ }
95171
96172 /**
97173 * Get microprocessor name
....@@ -103,10 +179,10 @@
103179 }
104180
105181 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());
108184 try {
109
- LOG.debug("Interface mac: {}", printMacAddress(network.getHardwareAddress()));
185
+ log.debug("Interface mac: {}", printMacAddress(network.getHardwareAddress()));
110186 } catch (SocketException e) {
111187 // Silent
112188 }