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 + "\n");
 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("\n==========================");
 48 
 49         int MacIndex = 0;// 記錄網卡
 50         while (mt.find()) {
 51             MacIndex++;
 52             System.out.println(mt.group(1));
 53         }
 54         System.out.println("\n共" + MacIndex + "個網卡");
 55         if (MacIndex == 0) {
 56             System.out.println("沒有網卡");
 57             return;
 58         }
 59 
 60         System.out.println("\n==========================");
 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("\n可用網卡共:" + MacUse + "個");
 78         System.out.println("\n==========================\n");
 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("\n第" + (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("\n=====================使用HashMap遍歷輸出===========================");
132         for (NetInfo h : mp.keySet()) {
133             System.out.println("\n網卡名字:" + mp.get(h) + "\n" + h);
134             System.out.println("\n-------------");
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("\n網卡名字:" + mp.get(h) + "\n" + h);
148                 NetId = h.getIPV4();
149                 System.out.println("\nIP:"+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 }

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM