Skip to content

Instantly share code, notes, and snippets.

@Urunov
Created August 23, 2022 11:06
Show Gist options
  • Save Urunov/cd8f3a9284eccc38f7bbe5c9ba37f7c8 to your computer and use it in GitHub Desktop.
Save Urunov/cd8f3a9284eccc38f7bbe5c9ba37f7c8 to your computer and use it in GitHub Desktop.
GET Client MAC and UUID in JAVA
public static String getMacAddress() throws Exception {
String macAddress = null;
String command = "ifconfig";
String osName = System.getProperty("os.name");
System.out.println("Operating System is " + osName);
if (osName.startsWith("Windows")) {
command = "ipconfig /all";
} else if (osName.startsWith("Linux") || osName.startsWith("Mac") || osName.startsWith("HP-UX")
|| osName.startsWith("NeXTStep") || osName.startsWith("Solaris") || osName.startsWith("SunOS")
|| osName.startsWith("FreeBSD") || osName.startsWith("NetBSD")) {
command = "ifconfig -a";
} else if (osName.startsWith("OpenBSD")) {
command = "netstat -in";
} else if (osName.startsWith("IRIX") || osName.startsWith("AIX") || osName.startsWith("Tru64")) {
command = "netstat -ia";
} else if (osName.startsWith("Caldera") || osName.startsWith("UnixWare") || osName.startsWith("OpenUNIX")) {
command = "ndstat";
} else {// Note: Unsupported system.
throw new Exception("The current operating system '" + osName + "' is not supported.");
}
Process pid = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream()));
Pattern p = Pattern.compile("([\\w]{1,2}(-|:)){5}[\\w]{1,2}");
while (true) {
String line = in.readLine();
System.out.println("line " + line);
if (line == null)
break;
Matcher m = p.matcher(line);
if (m.find()) {
macAddress = m.group();
break;
}
}
in.close();
return macAddress;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment