import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.StringTokenizer;
public final class NetworkUtils {
private final static int MACADDR_LENGTH = 17;
private final static String WIN_OSNAME = "Windows";
private final static String LINUX_OSNAME = "Linux";
private final static String OSX_OSNAME = "Mac OS X";
private final static String MACADDR_REG_EXP = "^[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}$";
private final static String WIN_MACADDR_EXEC = "ipconfig /all";
private final static String LINUX_MACADDR_EXEC = "ifconfig";
private final static String OSX_MACADDR_EXEC = "ifconfig";
public final static String getMacAddress() throws IOException {
String os = System.getProperty("os.name");
try {
if (os.startsWith(WIN_OSNAME)) {
return windowsParseMacAddress(windowsIpConfigCommand());
} else if (os.startsWith(LINUX_OSNAME)) {
return linuxParseMacAddress(linuxRunIfConfigCommand());
} else if (os.startsWith(OSX_OSNAME)) {
return osxParseMacAddress(osxRunIfConfigCommand());
} else {
throw new IOException("OS not supported : " + os);
}
} catch (Exception e) {
e.printStackTrace();
throw new IOException(e.getMessage());
}
}
/**
* OSX stuff
*/
private final static String osxParseMacAddress(String ipConfigOutput)
throws ParseException {
String localHost = null;
try {