zoukankan      html  css  js  c++  java
  • Java实现获取命令行中获取指定数据

    执行ipconfig /all获取主机所有网卡信息
    并分析这些字符串,提取出有效网卡(网卡名称,mac地址,ipv4地址,掩码,网关,dns)
    将网卡插入HashMap中,key是网卡的名称,value是网卡对象(包含mac和4个逻辑地址)
    请输入网卡的名称,程序通过map的get方法取出此名称对应的网卡对象
    根据网卡对象执行其方法getNetId()取出其网卡所在网络号进行打印
    getBroadId()取出其广播号进行打印
    2: 根据网卡的ip和掩码扫描所有这个子网中可能存在的邻居
    然后用ping ..方式进行验证此邻居是否存在,如果存在则将其加入
    网卡的邻居集合(HashSet)中
    3: 某些邻居有可能开启防火墙导致ping失败,所以验证其是否存在的
    恰当方式是先ping它一下,然后用arp -a查看这个邻居是否有arp回应
    如果存在arp条目则说明这个邻居是存在的.

      1 package day2020072501;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.InputStreamReader;
      5 import java.util.HashMap;
      6 import java.util.HashSet;
      7 import java.util.Scanner;
      8 import java.util.Set;
      9 import java.util.regex.Matcher;
     10 import java.util.regex.Pattern;
     11 
     12 public class Zzbds {
     13 
     14     public static String exeCmd(String commandStr) {
     15         BufferedReader br = null;
     16         try {
     17             Process p = Runtime.getRuntime().exec(commandStr);
     18             br = new BufferedReader(new InputStreamReader(p.getInputStream()));
     19             String line = null;
     20             StringBuilder sb = new StringBuilder();
     21             while ((line = br.readLine()) != null) {
     22                 sb.append(line + "
    ");
     23             }
     24             // System.out.println(sb.toString());
     25             return sb.toString();
     26         } catch (Exception e) {
     27             e.printStackTrace();
     28         } finally {
     29             if (br != null) {
     30                 try {
     31                     br.close();
     32                 } catch (Exception e) {
     33                     e.printStackTrace();
     34                 }
     35             }
     36         }
     37         return commandStr;
     38     }
     39 
     40     public static void main(String[] args) {
     41         String str = exeCmd("ipconfig /all");
     42         String expr = "(.+适配器 +.+)\:"; // 找到所有网卡名字
     43         HashMap<NetInfo, String> mp = new HashMap<>(); // HashMap存储信息
     44 
     45         Pattern pt = Pattern.compile(expr); // 配对 P,和正则匹配
     46         Matcher mt = pt.matcher(str); // 开始匹配源字符串 matcher
     47         System.out.println("
    ==========================");
     48 
     49         int MacIndex = 0;// 记录网卡
     50         while (mt.find()) {
     51             MacIndex++;
     52             System.out.println(mt.group(1));
     53         }
     54         System.out.println("
    共" + MacIndex + "个网卡");
     55         if (MacIndex == 0) {
     56             System.out.println("没有网卡");
     57             return;
     58         }
     59 
     60         System.out.println("
    ==========================");
     61 
     62         Matcher mt1 = pt.matcher(str); // 开始匹配源字符串 matcher
     63         // System.out.println("可用网卡");
     64         int MacUse = 0;// 可以使用的网卡数量
     65         String[] MacArr = new String[10];// 存储网卡数组(可用网卡)
     66         while (mt1.find()) { // 循环遍历所有网卡
     67             // 判断是否可用
     68             if (NetWorkUtil.NetWorkavailable(mt1.group())) {
     69                 MacArr[MacUse] = mt1.group();
     70                 MacUse++;
     71                 // System.out.println(mt1.group());
     72             }
     73         }
     74         for (int i = 0; i < MacUse; i++) {
     75             System.out.println(MacArr[i]);
     76         }
     77         System.out.println("
    可用网卡共:" + MacUse + "个");
     78         System.out.println("
    ==========================
    ");
     79 
     80         // System.out.println("------------------------------------");
     81         // 打印出可用的网卡信息
     82         for (int j = 0; j < MacUse; j++) { // 使用(数组)循环,打印所有可用网卡的所有信息
     83             String MacInfo = "";// 可用的网卡信息
     84             String expr1 = "(" + MacArr[j] + "([\d\D]*))";
     85             System.out.println("
    第" + (j + 1) + "个是:" + MacArr[j]);
     86             Pattern pt1 = Pattern.compile(expr1);
     87             Matcher mt2 = pt1.matcher(str);
     88             if (mt2.find()) {
     89                 MacInfo = mt2.group(1);// 把查到的信息赋给变量MaxInfo
     90             }
     91             // System.out.println(MacInfo);
     92             System.out.println("---------------------可用网卡的具体信息如下(第" + (j + 1) + "个网卡)----------------");
     93             Pattern pt2 = Pattern.compile(" +描述(\. +)+: (.*)");
     94             Matcher mt3 = pt2.matcher(MacInfo);// 网卡名
     95             Pattern pt3 = Pattern.compile(" +物理地址(\. +)+: (.*)");
     96             Matcher mt4 = pt3.matcher(MacInfo);// 网卡地址
     97             Pattern pt5 = Pattern.compile(" +IPv4 地址( +\.)+ +: +(.*)\(");
     98             Matcher mt5 = pt5.matcher(MacInfo);// IP地址
     99             Pattern pt6 = Pattern.compile(" +子网掩码( +\.)+ +: +(.*)");
    100             Matcher mt6 = pt6.matcher(MacInfo);// 子网掩码
    101             Pattern pt7 = Pattern.compile(" +默认网关(\. +)+: (.*)");
    102             Matcher mt7 = pt7.matcher(MacInfo);// 网关
    103             Pattern pt8 = Pattern.compile(" +DNS 服务器( +\.)+ +: +(.*)");
    104             Matcher mt8 = pt8.matcher(MacInfo);// DNS
    105 
    106             String MacName = "";
    107             String MacIP = "";
    108             String IPV4 = "";
    109             String NetMask = "";
    110             String GateWay = "";
    111             String DNS = "";
    112 
    113             if (mt3.find() && mt4.find() && mt5.find() && mt6.find() && mt7.find() && mt8.find()) {
    114                 MacName = mt3.group(2);
    115                 MacIP = mt4.group(2);
    116                 IPV4 = mt5.group(2);
    117                 NetMask = mt6.group(2);
    118                 GateWay = mt7.group(2);
    119                 DNS = mt8.group(2);
    120                 mp.put(new NetInfo(MacName,MacIP, IPV4, NetMask, GateWay, DNS), MacName);
    121             }
    122             System.out.println("网卡名称:" + MacName.trim());
    123             System.out.println("网卡地址:" + MacIP.trim());
    124             System.out.println("IPV4地址:" + IPV4.trim());
    125             System.out.println("子网掩码:" + NetMask.trim());
    126             System.out.println("默认网关:" + GateWay.trim());
    127             System.out.println("DNS地址:" + DNS.trim());
    128 
    129         }
    130 
    131         System.out.println("
    =====================使用HashMap遍历输出===========================");
    132         for (NetInfo h : mp.keySet()) {
    133             System.out.println("
    网卡名字:" + mp.get(h) + "
    " + h);
    134             System.out.println("
    -------------");
    135         }
    136         
    137         System.out.println("======================");
    138         System.out.println("请输入网卡名:");
    139         //String inputMacName = new Scanner(System.in).next();//输入网卡名称
    140         //默认输入:VMware Virtual Ethernet Adapter for VMnet8
    141         
    142         String NetId = "";//记录IP
    143         String inputMacName ="VMware Virtual Ethernet Adapter for VMnet8";
    144         System.out.println("您输入的是:"+inputMacName);
    145         for (NetInfo h : mp.keySet()) {
    146             if((h.getMacName().trim()).equals(inputMacName)){
    147                 System.out.println("
    网卡名字:" + mp.get(h) + "
    " + h);
    148                 NetId = h.getIPV4();
    149                 System.out.println("
    IP:"+NetId); //打印出此IP(后面求出网络号、广播号)
    150             }
    151         }
    152         
    153         
    154         
    155         //分解数组
    156         String []netIPArr = NetId.split("\.");
    157         for(int i= 0;i<netIPArr.length;i++){
    158             System.out.println(netIPArr[i]);
    159         }
    160         
    161         //求网络号:
    162             System.out.println("网络号:"+netIPArr[0]+"."+netIPArr[1]+"."+netIPArr[2]+"."+0);
    163             System.out.println("广播号:"+netIPArr[0]+"."+netIPArr[1]+"."+netIPArr[2]+"."+255);
    164         
    165         //访问所有邻居
    166         HashSet<String> nei = new HashSet<>();//存储所有可达的邻居
    167         for(int i= 1;i<5;i++){
    168             String str1 = exeCmd("ping "+netIPArr[0]+"."+netIPArr[1]+"."+netIPArr[2]+"."+i);
    169             System.out.println(str1);
    170             //判断是否Ping 通
    171             Pattern pt9 = Pattern.compile("TTL");
    172             Matcher mt9 = pt9.matcher(str1);
    173             if (mt9.find()){//如果能ping 通,直接加入到set集合内
    174                 //System.out.println(netIPArr[0]+"."+netIPArr[1]+"."+netIPArr[2]+"."+i);
    175                 nei.add(netIPArr[0]+"."+netIPArr[1]+"."+netIPArr[2]+"."+i);//存储
    176             }else{//如果ping 不同,使用arp 查看回应
    177                 String str2 = exeCmd("arp -a");
    178                 Pattern pt10 = Pattern.compile(netIPArr[0]+"."+netIPArr[1]+"."+netIPArr[2]+"."+i);
    179                 Matcher mt10 = pt10.matcher(str2);
    180                 if (mt10.find()){//如果arp 返回数据,也加入到set集合内
    181                     nei.add(netIPArr[0]+"."+netIPArr[1]+"."+netIPArr[2]+"."+i);//存储
    182                 }
    183             }
    184         }
    185     
    186         //输出所有可达的邻居
    187         System.out.println("所有可达的邻居:");
    188         for(String s : nei){
    189             System.out.println(s);
    190         }
    191 
    192     }
    193 }

     

     

  • 相关阅读:
    文本框改造之多选下拉控件
    多附件上传控件
    Linq to Sql:更新之属性遍历法
    如何在HTML5页面中启动本地的App? 下面的方法应该可以。
    Nodejs 学习笔记-相片整理Demo(二)
    Nodejs 学习笔记-相片整理Demo(一)
    前端学习笔记一:什么是W3C?
    网页嵌入调用 全国各城市天气代码
    html页面清除缓存
    判断鼠标动作,可以给鼠标在标签不同区域的动作分别写不同的效果
  • 原文地址:https://www.cnblogs.com/lwl80/p/13383047.html
Copyright © 2011-2022 走看看