一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java如何在命令行中獲取指定數(shù)據(jù)

Java如何在命令行中獲取指定數(shù)據(jù)

2020-07-30 14:38勤快的懶羊羊 Java教程

這篇文章主要介紹了Java如何在命令行中獲取指定數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1.執(zhí)行ipconfig /all獲取主機(jī)所有網(wǎng)卡信息

并分析這些字符串,提取出有效網(wǎng)卡(網(wǎng)卡名稱,mac地址,ipv4地址,掩碼,網(wǎng)關(guān),dns)

將網(wǎng)卡插入HashMap中,key是網(wǎng)卡的名稱,value是網(wǎng)卡對(duì)象(包含mac和4個(gè)邏輯地址)

請(qǐng)輸入網(wǎng)卡的名稱,程序通過map的get方法取出此名稱對(duì)應(yīng)的網(wǎng)卡對(duì)象

根據(jù)網(wǎng)卡對(duì)象執(zhí)行其方法getNetId()取出其網(wǎng)卡所在網(wǎng)絡(luò)號(hào)進(jìn)行打印

getBroadId()取出其廣播號(hào)進(jìn)行打印

2.根據(jù)網(wǎng)卡的ip和掩碼掃描所有這個(gè)子網(wǎng)中可能存在的鄰居

然后用ping ..方式進(jìn)行驗(yàn)證此鄰居是否存在,如果存在則將其加入

網(wǎng)卡的鄰居集合(HashSet)中

3.某些鄰居有可能開啟防火墻導(dǎo)致ping失敗,所以驗(yàn)證其是否存在的

恰當(dāng)方式是先ping它一下,然后用arp -a查看這個(gè)鄰居是否有arp回應(yīng)

如果存在arp條目則說明這個(gè)鄰居是存在的.

代碼實(shí)例

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

Java如何在命令行中獲取指定數(shù)據(jù)

Java如何在命令行中獲取指定數(shù)據(jù)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.cnblogs.com/lwl80/p/13383047.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 俺去俺来也在线www色官网 | 国内精品九一在线播放 | 肉大捧一进一出视频免费播放 | 久久这里只精品国产99re66 | 亚洲另类第一页 | 朝鲜美女免费一级毛片 | 精彩国产萝视频在线 | 东北恋哥在线播放免费播放 | 四虎影视入口 | 韩国最新三级网站在线播放 | 91在线精品老司机免费播放 | 99手机在线视频 | 办公室出轨秘书高h | 大胆国模一区二区三区伊人 | 亚洲国产中文字幕在线视频综合 | 四虎永久免费地址 | 无限资源在线观看8 | 成人高清网站 | 国产免费大片 | 国产成人99精品免费观看 | 国产永久免费爽视频在线 | 国产嫩草视频 | 麻豆网站在线看 | 亚洲免费精品视频 | 美女毛片视频 | 惩罚狠h调教灌满 | 三级视频中文字幕 | 99精品国产自产在线观看 | 爱情岛论坛自拍永久入口 | 欧美sq| 国产网站免费观看 | 不良小说 | 青草青青在线 | 亚洲精品九色在线网站 | xxx黑人又大粗又长 xxxx性欧美极品另类 | 欧美成人精品第一区二区三区 | 青草青视频 | 双性总裁被调教1v1 双性双根 | 欧美男同videos | 91视在线国内在线播放酒店 | 9999视频|