소스 1) FindMac.java
import java.net.InetAddress;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public final class FindMac {
public final static String getMacAddress() {
String mac = "";
String os = System.getProperty("os.name");
try {
if (os.startsWith("Windows")) {
mac = windowsParseMacAddress(windowsRunIpConfigCommand());
} else if (os.startsWith("Linux")) {
mac = linuxParseMacAddress(linuxRunIfConfigCommand());
} else if (os.startsWith("Mac OS X")) {
mac = osxParseMacAddress(osxRunIfConfigCommand());
}
} catch (Exception ex) {
ex.printStackTrace();
}
return mac;
}
/*
* Linux stuff
*/
private final static String linuxParseMacAddress(String ipConfigResponse)
throws ParseException {
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (java.net.UnknownHostException ex) {
ex.printStackTrace();
throw new ParseException(ex.getMessage(), 0);
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
String lastMacAddress = null;
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
boolean containsLocalHost = line.indexOf(localHost) >= 0;
// see if line contains IP address
if (containsLocalHost && lastMacAddress != null) {
return lastMacAddress;
}
// see if line contains MAC address
int macAddressPosition = line.indexOf("HWaddr");
if (macAddressPosition <= 0)
continue;
String macAddressCandidate = line.substring(macAddressPosition + 6)
.trim();
if (linuxIsMacAddress(macAddressCandidate)) {
lastMacAddress = macAddressCandidate;
continue;
}
}
ParseException ex = new ParseException("cannot read MAC address for "
+ localHost + " from [" + ipConfigResponse + "]", 0);
ex.printStackTrace();
throw ex;
}
private final static boolean linuxIsMacAddress(String macAddressCandidate) {
Pattern macPattern = Pattern
.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
Matcher m = macPattern.matcher(macAddressCandidate);
return m.matches();
}
private final static String linuxRunIfConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec("ifconfig");
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for (;;) {
int c = stdoutStream.read();
if (c == -1)
break;
buffer.append((char) c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/*
* Windows stuff
*/
private final static String windowsParseMacAddress(String ipConfigResponse)
throws ParseException {
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (java.net.UnknownHostException ex) {
ex.printStackTrace();
throw new ParseException(ex.getMessage(), 0);
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
String lastMacAddress = null;
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
// see if line contains IP address
if (line.endsWith(localHost) && lastMacAddress != null) {
return lastMacAddress;
}
// see if line contains MAC address
int macAddressPosition = line.indexOf(":");
if (macAddressPosition <= 0)
continue;
String macAddressCandidate = line.substring(macAddressPosition + 1)
.trim();
if (windowsIsMacAddress(macAddressCandidate)) {
lastMacAddress = macAddressCandidate;
continue;
}
}
ParseException ex = new ParseException("cannot read MAC address from ["
+ ipConfigResponse + "]", 0);
ex.printStackTrace();
throw ex;
}
private final static boolean windowsIsMacAddress(String macAddressCandidate) {
Pattern macPattern = Pattern
.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
Matcher m = macPattern.matcher(macAddressCandidate);
return m.matches();
}
private final static String windowsRunIpConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec("ipconfig /all");
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for (;;) {
int c = stdoutStream.read();
if (c == -1)
break;
buffer.append((char) c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/*
* Mac OS X Stuff
*/
private final static String osxParseMacAddress(String ipConfigResponse)
throws ParseException {
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (java.net.UnknownHostException ex) {
ex.printStackTrace();
throw new ParseException(ex.getMessage(), 0);
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
boolean containsLocalHost = line.indexOf(localHost) >= 0;
// see if line contains MAC address
int macAddressPosition = line.indexOf("ether");
if (macAddressPosition != 0)
continue;
String macAddressCandidate = line.substring(macAddressPosition + 6)
.trim();
if (osxIsMacAddress(macAddressCandidate)) {
return macAddressCandidate;
}
}
ParseException ex = new ParseException("cannot read MAC address for "
+ localHost + " from [" + ipConfigResponse + "]", 0);
ex.printStackTrace();
throw ex;
}
private final static boolean osxIsMacAddress(String macAddressCandidate) {
Pattern macPattern = Pattern
.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
Matcher m = macPattern.matcher(macAddressCandidate);
return m.matches();
}
private final static String osxRunIfConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec("ifconfig");
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for (;;) {
int c = stdoutStream.read();
if (c == -1)
break;
buffer.append((char) c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/*
* Main
*/
public final static void main(String[] args) {
try {
System.out.println("Network info:");
System.out.println(" Operating System: "
+ System.getProperty("os.name"));
System.out.println(" IP/Localhost: "
+ InetAddress.getLocalHost().getHostAddress());
System.out.println(" MAC Address: " + getMacAddress());
} catch (Throwable t) {
t.printStackTrace();
}
}
}
소스 2) MAC_TEST.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
class MAC_TEST {
static Pattern macPattern = Pattern
.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
// ------------------------------------------------------------------------------------------------
private static List getWindowsMACAddresses() {
try {
Process conf = Runtime.getRuntime().exec("ipconfig /all");
BufferedReader input = new BufferedReader(new InputStreamReader(
conf.getInputStream()));
return getMACAddresses(input);
} catch (Exception e) {
System.err.println("Error Reading Windows MAC Address.");
}
return new ArrayList(1);
}
// -----------------------------------------------------------------------------------------------
private static List getLinuxMACAddresses() {
try {
Process conf = Runtime.getRuntime().exec("/sbin/ifconfig");
BufferedReader input = new BufferedReader(new InputStreamReader(
conf.getInputStream()));
return getMACAddresses(input);
} catch (Exception e) {
System.err.println("Error Reading Linux MAC Address.");
}
return new ArrayList(1);
}
// -----------------------------------------------------------------------------------------------
private static List getHPUXMACAddresses() {
try {
Process conf = Runtime.getRuntime().exec("/etc/lanscan");
BufferedReader input = new BufferedReader(new InputStreamReader(
conf.getInputStream()));
return getMACAddresses(input);
} catch (Exception e) {
System.err.println("Error Reading HPUX MAC Address.");
}
return new ArrayList(1);
}
// -------------------------------------------------------------
private static List getSolarisMACAddresses() {
try {
List rtc = new ArrayList(1);
Process conf = Runtime.getRuntime().exec(
"/usr/sbin/arp "
+ InetAddress.getLocalHost().getHostAddress());
BufferedReader input = new BufferedReader(new InputStreamReader(
conf.getInputStream()));
rtc.addAll(getMACAddresses(input));
input.close();
input = null;
conf = null;
// Solaris reports MAC address without first 0, change the pattern
// at re-test
macPattern = Pattern
.compile("[0-9a-fA-F][-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
conf = Runtime.getRuntime().exec(
"/usr/sbin/arp "
+ InetAddress.getLocalHost().getHostAddress());
input = new BufferedReader(new InputStreamReader(conf
.getInputStream()));
rtc.addAll(getMACAddresses(input));
// Revert pattern
macPattern = Pattern
.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
return rtc;
} catch (Exception e) {
System.err.println("Error Reading Solaris MAC Address.");
}
return new ArrayList(1);
}
// -------------------------------------------------------------
private static List getMACAddresses(BufferedReader input) throws Exception {
List MACs = new ArrayList(1);
String theLine;
while ((theLine = input.readLine()) != null) {
String[] ss = macPattern.split(theLine);
for (int p = 0; p < ss.length; p++) {
String s = theLine.substring(
theLine.indexOf(ss[p]) + ss[p].length()).trim();
if (!s.equals("")) {
String s1 = s.replaceAll("-", ":");
String s2 = s1.substring(0, s1.lastIndexOf(":") + 3);
if (s2.length() == 16 || s2.length() == 17) {
MACs.add(s2);
}
}
}
}
return MACs;
}
// ----------------------------------------------
public static void main(String[] args) {
try {
System.out
.println("WINDOWS ... Found the following MAC Addresses: ");
List MACS = getWindowsMACAddresses();
System.out.println("*-----------------*");
for (int i = 0; i < MACS.size(); i++) {
System.out.println("|" + MACS.get(i) + "|");
}
System.out.println("*-----------------*");
System.out.println(" ");
// ------------------------------------------------------------------------------------------------------------------
System.out.println("Linux ... Found the following MAC Addresses: ");
MACS = getLinuxMACAddresses();
System.out.println("*-----------------*");
for (int i = 0; i < MACS.size(); i++) {
System.out.println("|" + MACS.get(i) + "|");
}
System.out.println("*-----------------*");
System.out.println(" ");
// ------------------------------------------------------------------------------------------------------------------
System.out
.println("Solaris ... Found the following MAC Addresses: ");
MACS = getSolarisMACAddresses();
System.out.println("*-----------------*");
for (int i = 0; i < MACS.size(); i++) {
System.out.println("|" + MACS.get(i) + "|");
}
System.out.println("*-----------------*");
System.out.println(" ");
// ------------------------------------------------------------------------------------------------------------------
System.out.println("HPUX ... Found the following MAC Addresses: ");
MACS = getHPUXMACAddresses();
System.out.println("*-----------------*");
for (int i = 0; i < MACS.size(); i++) {
System.out.println("|" + MACS.get(i) + "|");
}
System.out.println("*-----------------*");
System.out.println(" ");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
댓글 없음:
댓글 쓰기