Ping.java
5.6 KB
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
package com.sitech.util;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import com.adventnet.utils.agent.RunCmd;
/**
* Ping工具类
*
* @author LINXC
*/
public class Ping {
public static Logger logger= Logger.getLogger(Ping.class);
public static final String STATE = "STATE";
public static final String MINRTT = "MINRTT";
public static final String AVGRTT = "AVGRTT";
public static final String MAXRTT = "MAXRTT";
public static boolean ping1(String ipAddr) {
boolean isReacheable = false;
Map<String, String> result = ping2(ipAddr);
if (result.get(STATE) != null && result.get(STATE).equals("UP")) {
isReacheable = true;
}
logger.info("ping " + ipAddr + ":\t" + isReacheable);
return isReacheable;
}
public static Map<String, String> ping2(String ipAddr) {
Map<String, String> result = new HashMap<String, String>();
try {
result = new PingUtils().excute(ipAddr);
} catch (Exception e) {
logger.error("Exception while ping " + ipAddr, e);
}
logger.info("ping " + ipAddr + ":\t" + JSONUtil.toJSON(result));
return result;
}
public static void main(String[] args) {
// if (args.length < 1 || args[0].trim().equals("-h")) {
// System.err.println("Usage: com.sitech.util.Ping <remote-ip>");
// System.exit(1);
// }
// Ping.ping1(args[0]);
Ping.ping1("172.21.1.171");
}
}
class PingUtils {
public Map<String, String> excute(String ipAddr) {
Map<String, String> map = new HashMap<String, String>();
String os = System.getProperty("os.name").toLowerCase();
System.out.println("-- os:\t" + os);
String command = "";
if (os.indexOf("windows") >= 0) {
command = "ping -n 3 " + ipAddr;
} else if (os.indexOf("aix") >= 0) {
command = "ping " + ipAddr + " 9 3";
} else if (os.indexOf("sunos") >= 0 || os.indexOf("solaris") >= 0) {
command = "/usr/sbin/ping -s " + ipAddr + " 9 5";
} else if (os.indexOf("hp-ux") >= 0) {
command = "ping " + ipAddr + " -n 3 -m 3";
} else if (os.indexOf("linux") >= 0) {
command = "/bin/ping -c 3 -w 3 " + ipAddr;
} else if (os.indexOf("mac") >= 0) {
command = "/sbin/ping -c 3 " + ipAddr;
}
System.out.println(command);
String result = excuteCmd(command);
System.out.println(result);
String state = "DOWN";
String min = "-1";
String avg = "-1";
String max = "-1";
if (os.indexOf("windows") >= 0) {
if (result != null && result.indexOf("100% loss") < 0) {
state = "UP";
result = result.substring(result.indexOf("Minimum"));
String[] temp = result.split(",");
min = temp[0].replace("Minimum =", "").replace("ms", "").trim();
avg = temp[1].replace("Maximum =", "").replace("ms", "").trim();
max = temp[2].replace("Average =", "").replace("ms", "").trim();
}
} else if (os.indexOf("aix") >= 0) {
if (result != null && result.indexOf("100% packet loss") < 0) {
state = "UP";
result = result.substring(result.indexOf("min/avg/max"));
String[] temp1 = result.split("=");
temp1[1] = temp1[1].replace("ms", "").trim();
String[] temp2 = temp1[1].split("/");
min = temp2[0].trim();
avg = temp2[1].trim();
max = temp2[2].trim();
}
} else if (os.indexOf("sunos") >= 0 || os.indexOf("solaris") >= 0) {
if (result != null && result.indexOf("100% packet loss") < 0) {
state = "UP";
result = result.substring(result.indexOf("min/avg/max/stddev"));
String[] temp1 = result.split("=");
temp1[1] = temp1[1].replace("ms", "").trim();
String[] temp2 = temp1[1].split("/");
min = temp2[0].trim();
avg = temp2[1].trim();
max = temp2[2].trim();
}
} else if (os.indexOf("hp-ux") >= 0) {
if (result != null && result.indexOf("100% packet loss") < 0) {
state = "UP";
result = result.substring(result.indexOf("min/avg/max"));
String[] temp1 = result.split("=");
temp1[1] = temp1[1].replace("ms", "").trim();
String[] temp2 = temp1[1].split("/");
min = temp2[0].trim();
avg = temp2[1].trim();
max = temp2[2].trim();
}
} else if (os.indexOf("linux") >= 0) {
if (result != null && result.indexOf("100% packet loss") < 0) {
state = "UP";
result = result.substring(result.indexOf("min/avg/max/mdev"));
String[] temp1 = result.split("=");
temp1[1] = temp1[1].replace("ms", "").trim();
String[] temp2 = temp1[1].split("/");
min = temp2[0].trim();
avg = temp2[1].trim();
max = temp2[2].trim();
}
} else if (os.indexOf("mac") >= 0) {
if (result != null && result.indexOf("100.0% packet loss") < 0) {
state = "UP";
result = result.substring(result.indexOf("min/avg/max/stddev"));
String[] temp1 = result.split("=");
temp1[1] = temp1[1].replace("ms", "").trim();
String[] temp2 = temp1[1].split("/");
min = temp2[0].trim();
avg = temp2[1].trim();
max = temp2[2].trim();
}
}
map.put(Ping.STATE, state);
map.put(Ping.AVGRTT, avg);
map.put(Ping.MINRTT, min);
map.put(Ping.MAXRTT, max);
return map;
}
private String excuteCmd(String command) {
// 超时时间60秒
long timeout = 60000;
RunCmd runcmd = new RunCmd(command);
long startTime = new Date().getTime();
runcmd.start();
while (runcmd.isAlive()) {
try {
Thread.sleep(1000L);
} catch (Exception e) {
}
if (new Date().getTime() > startTime + timeout) {
runcmd.stopCommand();
return null;
}
}
return runcmd.stdout.toString();
}
}