首页 > 代码库 > 调用cmd命令行命令(借鉴)
调用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
调用cmd命令行命令(借鉴)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。