JAVA MacUtil

来源:互联网 发布:c语言编程简易小游戏 编辑:程序博客网 时间:2024/05/29 14:10
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"));}}

0 0
原创粉丝点击