Roberto Sánchez
2014-09-18 a4b1e4c32927208e92ca9895fa780e18051d3932
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package net.curisit.securis.utils;
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 net.curisit.securis.SeCurisException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
 * Retrieve HW info
 * 
 * @author cesarcalvo
 * 
 */
public class HWInfo {
   private static final Logger LOG = LogManager.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<String> getMACAddress() throws SeCurisException {
       List<byte[]> macs = new ArrayList<byte[]>();
       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<String> macAddresses = new ArrayList<String>();
           for (byte[] mac : macs) {
               macAddresses.add(printMacAddress(mac));
           }
           LOG.debug("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();
   }
}