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 MacUtil 获取mac地址 工具类
- JAVA工具类---MACUtil
- Java.util.date 与 java.sql.date区别和转换
- java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.isPresent(Ljava/lang/String;Ljava/l
- java.util.ArrayList.clone()是浅层拷贝
- java.util.concurrent.ThreadPoolExecutor实现机制简介
- java.util.Queue用法
- mac系统开发java需要注意
- java.util.Locale
- java.util.concurrent.Executors学习
- 关于运行期异常:Failed to instantiate [java.util.List]: Specified class is an interface
- Java多线程工具包java.util.concurrent---Atomic
- Mac下Java JDK的下载安装和配置
- java.util.ConcurrentModificationException 并发修改异常处理
- java获取mac地址
- java.util.concurrent.locks与synchronized
- 日期转换处理类 DateUtil.java
- Java 2源码解读:java.util.ArrayList
- java.util.Date和java.sql.Date之间的转换
- mybatis异常invalid comparison: java.util.Date and java.lang.String