package ARP_Soft;
import java.net.InetAddress;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.StringTokenizer;
public final class Get_PC_Info {
public static String getMacAddress(){
String os = System.getProperty("os.name");
try {
if (os.startsWith("Windows")) {
try {
return windowsParseMacAddress(windowsRunIpConfigCommand());
} catch (IOException e) {
e.printStackTrace();
}
} else if (os.startsWith("Linux")) {
try {
return linuxParseMacAddress(linuxRunIfConfigCommand());
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("unknown operating system: " + os);
}
} catch (ParseException ex) {
ex.printStackTrace();
}
return null;
}
/*
* Linux stuff
*/
private 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 static boolean linuxIsMacAddress(String macAddressCandidate) {
if (macAddressCandidate.length() != 17)
return false;
return true;
}
private 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 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 static boolean windowsIsMacAddress(String macAddressCandidate) {
if (macAddressCandidate.length() != 17)
return false;
return true;
}
private 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;
}
////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
try {
System.out.println("本PC信息如下:");
System.out.println("操作系统: "+System.getProperty("os.name"));
System.out.println("IP 地 址: "+InetAddress.getLocalHost().getHostAddress());
System.out.println("物理地址: "+getMacAddress());
} catch (Throwable t) {
t.printStackTrace();
}
}
}