java通过IP地址获取MAC地址

来源:互联网 发布:淘宝联盟转化优惠券 编辑:程序博客网 时间:2024/04/29 07:27

什么都不多说直接上代码....其实很简单...可以到Windows的cmd中测试下是否可以获取成功


public String getMACAddress(String ip){          String str = "";          String macAddress = "";          try {          String sep=System.getProperties().getProperty("file.separator");        String scancmd="nbtstat -A";        if(sep.equals("/"))        scancmd="nmblookup -A";            Process p = Runtime.getRuntime().exec(scancmd + 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());                          break;                      }                  }              }          } catch (IOException e) {              e.printStackTrace(System.out);          }          return macAddress;      }  

	
				
		
原创粉丝点击