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条目则说明这个邻居是存在的.
代码实例
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 = "(.+适配器 +.+)\\:" ; // 找到所有网卡名字
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] + "([\\d\\D]*))" ;
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);
nei.add(netIPArr[ 0 ]+ "." +netIPArr[ 1 ]+ "." +netIPArr[ 2 ]+ "." +i); //存储
}
}
}
//输出所有可达的邻居
System.out.println( "所有可达的邻居:" );
for (String s : nei){
System.out.println(s);
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/lwl80/p/13383047.html