-
Notifications
You must be signed in to change notification settings - Fork 41
/
PingUtil.java
345 lines (321 loc) · 11.2 KB
/
PingUtil.java
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package com.reactlibrary.LHPing;
import android.util.ArrayMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.regex.Pattern;
/**
* 类描述:手机ping工具类<br>
* 权限 <uses-permission android:name="android.permission.INTERNET"/> <br>
* 由于涉及网络,建议异步操作<br>
*/
public class PingUtil {
private static final String ipRegex =
"((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|(" +
"(1\\d{2})|([1-9]?\\d))))";
/**
* 获取由ping url得到的IP地址
*
* @param url 需要ping的url地址
* @return url的IP地址 如 192.168.0.1
*/
public static String getIPFromUrl(String url) {
String domain = getDomain(url);
if (null == domain) {
return null;
}
if (isMatch(ipRegex, domain)) {
return domain;
}
String pingString = ping(createSimplePingCommand(1, 100, domain));
if (null != pingString) {
try {
String tempInfo = pingString.substring(pingString.indexOf("from") + 5);
return tempInfo.substring(0, tempInfo.indexOf("icmp_seq") - 2);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 获取ping最小RTT值
*
* @param url 需要ping的url地址
* @return 最小RTT值,单位 ms 注意:-1是默认值,返回-1表示获取失败
*/
public static int getMinRTT(String url) {
return getMinRTT(url, 1, 100);
}
/**
* 获取ping的平均RTT值
*
* @param url 需要ping的url地址
* @return 平均RTT值,单位 ms 注意:-1是默认值,返回-1表示获取失败
*/
public static int getAvgRTT(String url) {
return getAvgRTT(url, 1, 100);
}
/**
* 获取ping的最大RTT值
*
* @param url 需要ping的url地址
* @return 最大RTT值,单位 ms 注意:-1是默认值,返回-1表示获取失败
*/
public static int getMaxRTT(String url) {
return getMaxRTT(url, 1, 100);
}
/**
* 获取ping的RTT的平均偏差
*
* @param url 需要ping的url地址
* @return RTT平均偏差,单位 ms 注意:-1是默认值,返回-1表示获取失败
*/
public static int getMdevRTT(String url) {
return getMdevRTT(url, 1, 100);
}
/**
* 获取ping url的最小RTT
*
* @param url 需要ping的url地址
* @param count 需要ping的次数
* @param timeout 需要ping的超时,单位ms
* @return 最小RTT值,单位 ms 注意:-1是默认值,返回-1表示获取失败
*/
public static int getMinRTT(String url, int count, int timeout) {
String domain = getDomain(url);
if (null == domain) {
return -1;
}
String pingString = ping(createSimplePingCommand(count, timeout, domain));
if (null != pingString) {
try {
String tempInfo = pingString.substring(pingString.indexOf("min/avg/max/mdev") + 19);
String[] temps = tempInfo.split("/");
return Math.round(Float.valueOf(temps[0]));
} catch (Exception e) {
e.printStackTrace();
}
}
return -1;
}
/**
* 获取ping url的平均RTT
*
* @param domain 需要ping的domain
* @param count 需要ping的次数
* @param timeout 需要ping的超时时间,单位 ms
* @return 平均RTT值,单位 ms 注意:-1是默认值,返回-1表示获取失败
*/
public static int getAvgRTT(String domain, int count, int timeout) {
String pingString = ping(createSimplePingCommand(count, timeout, domain), timeout);
String tempInfo = pingString.substring(pingString.indexOf("min/avg/max/mdev") + 19);
String[] temps = tempInfo.split("/");
return Math.round(Float.valueOf(temps[1]));
}
/**
* 获取ping url的最大RTT
*
* @param url 需要ping的url地址
* @param count 需要ping的次数
* @param timeout 需要ping的超时时间,单位ms
* @return 最大RTT值,单位 ms 注意:-1是默认值,返回-1表示获取失败
*/
public static int getMaxRTT(String url, int count, int timeout) {
String domain = getDomain(url);
if (null == domain) {
return -1;
}
String pingString = ping(createSimplePingCommand(count, timeout, domain));
if (null != pingString) {
try {
String tempInfo = pingString.substring(pingString.indexOf("min/avg/max/mdev") + 19);
String[] temps = tempInfo.split("/");
return Math.round(Float.valueOf(temps[2]));
} catch (Exception e) {
e.printStackTrace();
}
}
return -1;
}
/**
* 获取RTT的平均偏差
*
* @param url 需要ping的url
* @param count 需要ping的次数
* @param timeout 需要ping的超时时间,单位ms
* @return RTT平均偏差,单位 ms 注意:-1是默认值,返回-1表示获取失败
*/
public static int getMdevRTT(String url, int count, int timeout) {
String domain = getDomain(url);
if (null == domain) {
return -1;
}
String pingString = ping(createSimplePingCommand(count, timeout, domain));
if (null != pingString) {
try {
String tempInfo = pingString.substring(pingString.indexOf("min/avg/max/mdev") + 19);
String[] temps = tempInfo.split("/");
return Math.round(Float.valueOf(temps[3].replace(" ms", "")));
} catch (Exception e) {
e.printStackTrace();
}
}
return -1;
}
/**
* 获取ping url的丢包率,浮点型
*
* @param url 需要ping的url地址
* @return 丢包率 如50%可得 50,注意:-1是默认值,返回-1表示获取失败
*/
public static float getPacketLossFloat(String url) {
String packetLossInfo = getPacketLoss(url);
if (null != packetLossInfo) {
try {
return Float.valueOf(packetLossInfo.replace("%", ""));
} catch (Exception e) {
e.printStackTrace();
}
}
return -1;
}
/**
* 获取ping url的丢包率,浮点型
*
* @param url 需要ping的url地址
* @param count 需要ping的次数
* @param timeout 需要ping的超时时间,单位 ms
* @return 丢包率 如50%可得 50,注意:-1是默认值,返回-1表示获取失败
*/
public static float getPacketLossFloat(String url, int count, int timeout) {
String packetLossInfo = getPacketLoss(url, count, timeout);
if (null != packetLossInfo) {
try {
return Float.valueOf(packetLossInfo.replace("%", ""));
} catch (Exception e) {
e.printStackTrace();
}
}
return -1;
}
/**
* 获取ping url的丢包率
*
* @param url 需要ping的url地址
* @return 丢包率 x%
*/
public static String getPacketLoss(String url) {
return getPacketLoss(url, 1, 100);
}
/**
* 获取ping url的丢包率
*
* @param url 需要ping的url地址
* @param count 需要ping的次数
* @param timeout 需要ping的超时时间,单位ms
* @return 丢包率 x%
*/
public static String getPacketLoss(String url, int count, int timeout) {
String domain = getDomain(url);
if (null == domain) {
return null;
}
String pingString = ping(createSimplePingCommand(count, timeout, domain));
if (null != pingString) {
try {
String tempInfo = pingString.substring(pingString.indexOf("received,"));
return tempInfo.substring(9, tempInfo.indexOf("packet"));
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
// ********************以下是一些辅助方法********************//
private static String getDomain(String url) {
String domain = null;
try {
domain = URI.create(url).getHost();
} catch (Exception e) {
e.printStackTrace();
}
return domain;
}
private static boolean isMatch(String regex, String string) {
return Pattern.matches(regex, string);
}
private static String ping(String command) {
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
InputStream is = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while (reader.ready() && null != (line = reader.readLine())) {
sb.append(line);
sb.append("\n");
}
reader.close();
is.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != process) {
process.destroy();
}
}
return null;
}
private static String ping(String command, int timeout) {
Process process = null;
long startTime = System.currentTimeMillis();
try {
process = Runtime.getRuntime().exec(command);
InputStream is = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
boolean isBreak = false;
while (true) {
long currentTime = System.currentTimeMillis();
if (isBreak || (currentTime - startTime > timeout)) {
break;
}
if (reader.ready()) {
while (null != (line = reader.readLine())) {
sb.append(line);
sb.append("\n");
}
isBreak = true;
}
}
reader.close();
is.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != process) {
process.destroy();
}
}
return null;
}
private static String createSimplePingCommand(int count, int timeout, String domain) {
return "/system/bin/ping -c " + count + " -w " + timeout + " " + domain;
}
private static String createPingCommand(ArrayMap<String, String> map, String domain) {
String command = "/system/bin/ping";
int len = map.size();
for (int i = 0; i < len; i++) {
command = command.concat(" " + map.keyAt(i) + " " + map.get(map.keyAt(i)));
}
command = command.concat(" " + domain);
return command;
}
}