mi stava avendo un problema per ottenere l'indirizzo MAC di una macchina, che è stato risolto in this question utilizzando il codice seguente:Get MAC address in Java utilizzando getHardwareAddress non deterministico
Process p = Runtime.getRuntime().exec("getmac /fo csv /nh");
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
String line;
line = in.readLine();
String[] result = line.split(",");
System.out.println(result[0].replace('"', ' ').trim());
Tuttavia, vorrei sapere perché questo codice non funziona Ogni volta che legge l'indirizzo MAC, restituisce un valore diverso. Per prima cosa ho pensato che fosse perché getHash, magari usando un timestamp che non so ... Ma anche rimuovendoli i cambiamenti di risultato.
Codice
public static byte[] getMacAddress() {
try {
Enumeration<NetworkInterface> nwInterface = NetworkInterface.getNetworkInterfaces();
while (nwInterface.hasMoreElements()) {
NetworkInterface nis = nwInterface.nextElement();
if (nis != null) {
byte[] mac = nis.getHardwareAddress();
if (mac != null) {
/*
* Extract each array of mac address and generate a
* hashCode for it
*/
return mac;//.hashCode();
} else {
Logger.getLogger(Utils.class.getName()).log(Level.WARNING, "Address doesn't exist or is not accessible");
}
} else {
Logger.getLogger(Utils.class.getName()).log(Level.WARNING, "Network Interface for the specified address is not found.");
}
return null;
}
} catch (SocketException ex) {
Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
Esempio di stampa (sto stampando direttamente da array di byte, ma la sua abbastanza per vedere che diversi credo)
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
Grazie in anticipo
si stampa il default 'toString' di un array di byte. –