您的位置:首页 > 编程语言 > Java开发

JAVA MacUtil

2015-05-24 09:44 393 查看
package reflect;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class MacUtil {

public String getMACAddress2(String ip){

String str = "";
String macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("ping " + ip);

Process p = Runtime.getRuntime().exec("arp -a");
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf(ip) > 1) {
macAddress = str.substring(str.indexOf(ip) + 22, 41).toUpperCase();
if("00-00-00-00-00-00".equals(macAddress)){
macAddress = null;
}
ir.close();
input.close();
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}

public String getMACAddress(String ip){

String str = "";
String macAddress = "";
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(p.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());
ir.close();
input.close();
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
if(macAddress==null || "".equals(macAddress)){
macAddress = this.getMACAddress2(ip);
}
return macAddress;
}

public static void main(String[] args) {
MacUtil m = new MacUtil();
System.out.println(m.getMACAddress("192.168.0.123"));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java util macutil