调用cmd命令行命令(借鉴)
留待以后观看
————————————————————————————————————————————————————————————————————————————
public class IP_MAC_TypeHelper { /** * 控制台执行arp -a命令 * * @return * */ public static InputStream getInputStream() { Runtime rt = Runtime.getRuntime(); InputStream in = null; try { Process p = rt.exec("cmd.exe /c arp -a"); in = p.getInputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return in; } /** * 获取的字节流转成String * * @param in * @return */ public static String read(InputStream in) { InputStreamReader isr; try { isr = new InputStreamReader(in, "GBK"); BufferedReader br = new BufferedReader(isr); String inline = null; StringBuffer sb = new StringBuffer(); while ((inline = br.readLine()) != null) { // System.out.println(inline); sb.append(inline); } return sb.toString(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 格式化输出信息 * * @param msg * @return */ public static String[] getMsg(String msg) { //按换行截取 String[] tempmessage = msg.split("\r\n"); StringBuffer sb = new StringBuffer(); for (String s : tempmessage) { sb.append(s + " "); } String temp = sb.toString(); return temp.split(" {1,}"); } /** * 截取IP地址信息 * * @param msg * @return */ public static List<String> getIp(String[] msg) { List<String> list = new ArrayList<String>(); for (String s : msg) { boolean flag = s.matches("^[0-9]{1,3}(\\.[0-9]{1,3}){3}$");// 匹配IP的正则 if (flag) { list.add(s); } } return list; } /** * 截取MAC地址信息 * * @param msg * @return */ public static List<String> getMac(String[] msg) { List<String> list = new ArrayList<String>(); String regx = "^[a-zA-Z0-9]{2}(-[a-zA-Z0-9]{2}){5}$"; // 匹配MAC地址的正则 for (String s : msg) { if (s.matches(regx)) { list.add(s); } } return list; } /** * 截取类型信息 * * @param msg * @return */ public static List<String> getType(String[] msg) { List<String> list = new ArrayList<String>(); for (String s : msg) { if (s.contains("态")) { // 判断是否为指定字符 list.add(s); } } return list; } /** * 移除本机IP(包含网卡) * @param ipList * @return */ public static List<String> removeLocalIp(List<String> ipList) { List<String> ripList = new ArrayList<String>(); try { Enumeration<NetworkInterface> netInterfaces = NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface nif = netInterfaces.nextElement(); Enumeration<InetAddress> iparray = nif.getInetAddresses(); while (iparray.hasMoreElements()) { /* * System.out.println("IP:" + * iparray.nextElement().getHostAddress()); */ ripList.add(iparray.nextElement().getHostAddress()); } } } catch (Exception e) { System.out.println(e.getMessage()); } for (int i = 0; i < ipList.size() - 1; i++) { for (String rip : ripList) { if (ipList.get(i).equals(rip)) { ipList.remove(i); } } } return ipList; } /** * 获取对应的IP、MAC、类型 * @return */ public static List<Admin> getIp_Mac_Type() { List<Admin> aList = new ArrayList<Admin>(); InputStream in = getInputStream(); String message = read(in); String[] msg = getMsg(message); List<String> list_ip = removeLocalIp(getIp(msg)); List<String> list_mac = getMac(msg); List<String> list_type = getType(msg); for(int i = 0; i<list_ip.size(); i++){ Admin admin = new Admin(); admin.setIp(list_ip.get(i)); admin.setMac(list_mac.get(i)); admin.setType(list_type.get(i)); aList.add(admin); } for(Admin a:aList){ System.out.println(a.getIp()); } return aList; } }
我们知道在cmd命令行窗口中输入arp -a能得到局域网下所有IP,上述代码调用该命令得到所有IP,以上仅为借鉴。有待完善。。。
————————————————————————————————————————————————————————
版权所有,出自http://www.cnblogs.com/ytlds