/* StringTokenizer
* public StringTokenizer(String str,String delim, boolean returnTokens);
* public StringTokenizer(String str, String delim);
* public StringTokenizer(String str);
*/
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
String lastMacAddress = null;
//判断字符串中是否还有token
while (tokenizer.hasMoreTokens())
{
String line = tokenizer.nextToken().trim();
// see if line contains IP address
if (line.endsWith(localHost) && lastMacAddress != null)
{
System.out.println( "MAC_Address = " + lastMacAddress );
System.out.println( "IPA_ddress = " + localHost );
return lastMacAddress;
}
// see if line contains MAC address
int macAddressPosition = line.indexOf(":");
if (macAddressPosition <= 0)
continue;
String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
//System.out.println(macAddressCandidate);
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)
{
// TODO: use a smart regular expression
if (macAddressCandidate.length() != 17)
return false;
return true;
}
private final static String windowsRunIpConfigCommand() throws IOException
{
//run the command "ipconfig /all" in order to get the MAC INFO;
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 = new String((buffer.toString().getBytes("ISO8859_1")), "GB2312");
stdoutStream.close();
System.out.println("outputText = " + outputText );
return outputText;
}
private final static String getMacAddress(String remotePcIP)
{
String str="";
String macAddress="";
try
{
Process pp= Runtime.getRuntime().exec ("nbtstat -A " + remotePcIP);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
for (int i = 1; i <100; i++)
{
str=input.readLine();
if (str!=null)
{
if(str.indexOf("MAC Address")>1)
{
macAddress=str.substring(str.indexOf("MAC Address")+14,str.length());
break;
}
}
}
}