亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

Java如何在命令行中獲取指定數據

瀏覽:9日期:2022-08-27 18:00:09

1.執行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條目則說明這個鄰居是存在的.

代碼實例

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 = '(.+適配器 +.+):'; // 找到所有網卡名字 HashMap<NetInfo, String> mp = new HashMap<>(); // HashMap存儲信息 Pattern pt = Pattern.compile(expr); // 配對 P,和正則匹配 Matcher mt = pt.matcher(str); // 開始匹配源字符串 matcher System.out.println('n=========================='); int MacIndex = 0;// 記錄網卡 while (mt.find()) { MacIndex++; System.out.println(mt.group(1)); } System.out.println('n共' + MacIndex + '個網卡'); if (MacIndex == 0) { System.out.println('沒有網卡'); return; } System.out.println('n=========================='); Matcher mt1 = pt.matcher(str); // 開始匹配源字符串 matcher // System.out.println('可用網卡'); int MacUse = 0;// 可以使用的網卡數量 String[] MacArr = new String[10];// 存儲網卡數組(可用網卡) while (mt1.find()) { // 循環遍歷所有網卡 // 判斷是否可用 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可用網卡共:' + MacUse + '個'); System.out.println('n==========================n'); // System.out.println('------------------------------------'); // 打印出可用的網卡信息 for (int j = 0; j < MacUse; j++) { // 使用(數組)循環,打印所有可用網卡的所有信息 String MacInfo = '';// 可用的網卡信息 String expr1 = '(' + MacArr[j] + '([dD]*))'; System.out.println('n第' + (j + 1) + '個是:' + 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('---------------------可用網卡的具體信息如下(第' + (j + 1) + '個網卡)----------------'); Pattern pt2 = Pattern.compile(' +描述(. +)+: (.*)'); Matcher mt3 = pt2.matcher(MacInfo);// 網卡名 Pattern pt3 = Pattern.compile(' +物理地址(. +)+: (.*)'); Matcher mt4 = pt3.matcher(MacInfo);// 網卡地址 Pattern pt5 = Pattern.compile(' +IPv4 地址( +.)+ +: +(.*)('); Matcher mt5 = pt5.matcher(MacInfo);// IP地址 Pattern pt6 = Pattern.compile(' +子網掩碼( +.)+ +: +(.*)'); Matcher mt6 = pt6.matcher(MacInfo);// 子網掩碼 Pattern pt7 = Pattern.compile(' +默認網關(. +)+: (.*)'); Matcher mt7 = pt7.matcher(MacInfo);// 網關 Pattern pt8 = Pattern.compile(' +DNS 服務器( +.)+ +: +(.*)'); 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('網卡名稱:' + MacName.trim()); System.out.println('網卡地址:' + MacIP.trim()); System.out.println('IPV4地址:' + IPV4.trim()); System.out.println('子網掩碼:' + NetMask.trim()); System.out.println('默認網關:' + GateWay.trim()); System.out.println('DNS地址:' + DNS.trim()); } System.out.println('n=====================使用HashMap遍歷輸出==========================='); for (NetInfo h : mp.keySet()) { System.out.println('n網卡名字:' + mp.get(h) + 'n' + h); System.out.println('n-------------'); } System.out.println('======================'); System.out.println('請輸入網卡名:'); //String inputMacName = new Scanner(System.in).next();//輸入網卡名稱 //默認輸入: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網卡名字:' + mp.get(h) + 'n' + h);NetId = h.getIPV4();System.out.println('nIP:'+NetId); //打印出此IP(后面求出網絡號、廣播號) } } //分解數組 String []netIPArr = NetId.split('.'); for(int i= 0;i<netIPArr.length;i++){ System.out.println(netIPArr[i]); } //求網絡號: System.out.println('網絡號:'+netIPArr[0]+'.'+netIPArr[1]+'.'+netIPArr[2]+'.'+0); System.out.println('廣播號:'+netIPArr[0]+'.'+netIPArr[1]+'.'+netIPArr[2]+'.'+255); //訪問所有鄰居 HashSet<String> nei = new HashSet<>();//存儲所有可達的鄰居 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集合內//System.out.println(netIPArr[0]+'.'+netIPArr[1]+'.'+netIPArr[2]+'.'+i);nei.add(netIPArr[0]+'.'+netIPArr[1]+'.'+netIPArr[2]+'.'+i);//存儲 }else{//如果ping 不同,使用arp 查看回應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 返回數據,也加入到set集合內 nei.add(netIPArr[0]+'.'+netIPArr[1]+'.'+netIPArr[2]+'.'+i);//存儲} } } //輸出所有可達的鄰居 System.out.println('所有可達的鄰居:'); for(String s : nei){ System.out.println(s); } }}

Java如何在命令行中獲取指定數據

Java如何在命令行中獲取指定數據

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 成年人看的免费视频 | 亚洲乱人伦精品图片 | 亚洲精品一二三四区 | 成人爽a毛片在线视频 | 久久免费手机视频 | 99久久99久久精品免费看子 | 国产一区a | 国产亚洲精品网站 | 萝控精品福利视频一区 | 欧美日韩a级片 | 激情一区二区三区成人 | 日本一区二区三区不卡视频中文字幕 | 免费人成在线观看播放国产 | 免费高清特级毛片 | 日本红怡院在线 | α级毛片| 欧美一级欧美一级在线播放 | 亚洲精品国产一区二区三区四区 | 欧美成人免费xxx大片 | 俺来也俺来也天天夜夜视频 | 成人看片黄a在线观看 | 国产免费人成在线看视频 | 国产一在线精品一区在线观看 | 国产在线精品香蕉综合网一区 | 一区二区三区日韩精品 | 亚洲精品天堂在线观看 | 欧美一级毛片高清视频 | 亚洲 中文 欧美 日韩 在线人 | 国产精品久久久久网站 | 国产一区二区三区影院 | 人成在线免费视频 | 成年人免费黄色片 | 欧美另类视频videosbest18 | 日本高清视频在线观看 | 一级毛片在线免费看 | 99www综合久久爱com | 成人精品一区二区www | 国产精品v免费视频 | 中文字幕人成乱码在线观看 | 亚洲国产激情在线一区 | 日本在线观看不卡 |