Ping.java
4.8 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
package com.sitech.ismp.util;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import com.sitech.util.Formater;
/**
* Created by IntelliJ IDEA. User: LJ Date: 2010-2-23
*/
public class Ping {
private static Logger logger = Logger.getLogger(Ping.class);
public static Map<String, String> ping(String hostName) throws Exception {
logger.info("-- Begin ping " + hostName);
String strResult = execute(hostName);
logger.info("-- Ping " + hostName + ", Result=" + strResult);
String strOS = System.getProperty("os.name").toLowerCase();
logger.info("-- OS = " + strOS);
Map<String, String> result = null;
if (strOS.indexOf("windows") != -1) {
result = getWindowsData(strResult);
} else {
result = getHPUnixData(strResult);
}
return result;
}
public static String execute(String hostName) throws Exception {
String s = "";
String s1 = "";
String s2 = "";
String s3 = (new PingTraceUtil()).getResult(hostName,
PingTraceUtil.Ping);
String s4 = System.getProperty("os.name").toLowerCase();
if (s4.indexOf("windows") == -1) {
int i = s3.indexOf("---");
if (i != -1) {
s1 = s3.substring(i);
s = s3.substring(0, i);
}
i = s1.lastIndexOf("---");
if (i != -1) {
s2 = s1.substring(i + 3);
s1 = s1.substring(0, i + 3);
}
s3 = s + "<br/><br/>" + s1 + "<br/><br/>" + s2;
}
return s3;
}
private static Map<String, String> getWindowsData(String result) {
Map<String, String> hash = new HashMap<String, String>();
String STATE = "";
String MINRTT = "";
String AVGRTT = "";
String MAXRTT = "";
try {
if (result != null && result.indexOf("100% loss") != -1) {
STATE = "DOWN";
MINRTT = "-1";
AVGRTT = "-1";
MAXRTT = "-1";
} else {
STATE = "UP";
ByteArrayInputStream stream = new ByteArrayInputStream(result
.getBytes());
BufferedReader in = new BufferedReader(new InputStreamReader(
stream));
String line = "";
while ((line = in.readLine()) != null) {
if (line != null
&& line.toLowerCase().indexOf("average") > 0) {
String rttArray[] = line.toLowerCase().split(",");
MINRTT = rttArray[0].substring(
rttArray[0].indexOf("=") + 1, rttArray[0]
.indexOf("ms"));
MAXRTT = rttArray[1].substring(
rttArray[1].indexOf("=") + 1, rttArray[1]
.indexOf("ms"));
AVGRTT = rttArray[2].substring(
rttArray[2].indexOf("=") + 1, rttArray[2]
.indexOf("ms"));
break;
}
}
}
logger.info("Ping getWindowsData:STATE,MINRTT,AVGRTT,MAXRTT:"
+ STATE + "," + MINRTT.trim() + "," + AVGRTT.trim() + ","
+ MAXRTT.trim() + "," + Formater.getNowTime());
hash.put("STATE", STATE);
hash.put("MINRTT", MINRTT.trim());
hash.put("AVGRTT", AVGRTT.trim());
hash.put("MAXRTT", MAXRTT.trim());
} catch (Exception e) {
logger.error("Exception while getWindowsData:" + result, e);
}
return hash;
}
/**
* PING 172.21.1.71 (172.21.1.71) 56(84) bytes of data. 64 bytes from
* 172.21.1.71: icmp_seq=1 ttl=255 time=2.42 ms
*
* <br/><br/>--- 172.21.1.71 ping statistics ---<br/><br/> 1 packets
* transmitted, 1 received, 0% packet loss, time 2ms rtt min/avg/max/mdev =
* 2.426/2.426/2.426/0.000 ms
*
* @param strResult
* @return
*/
private static Map<String, String> getHPUnixData(String result) {
Map<String, String> hash = new HashMap<String, String>();
String STATE = "";
String MINRTT = "";
String AVGRTT = "";
String MAXRTT = "";
try {
String[] line = result.split("\n");
for (int i = 0; i < line.length; i++) {
logger.info("-- line" + i + ":" + line[i]);
if (line[i] != null
&& line[i].indexOf("100% packet loss") != -1) {
STATE = "DOWN";
MINRTT = "-1";
AVGRTT = "-1";
MAXRTT = "-1";
break;
} else if (line[i] != null && line[i].indexOf("avg") != -1) {
String total = line[i].substring(line[i].indexOf("=") + 1)
.trim();
String arr[] = total.split("/");
MINRTT = arr[0];
AVGRTT = arr[1];
MAXRTT = arr[2];
STATE = "UP";
break;
}
}
logger.info("Ping result:STATE,MINRTT,AVGRTT,MAXRTT:" + STATE + ","
+ MINRTT.trim() + "," + AVGRTT.trim() + "," + MAXRTT.trim()
+ "," + Formater.getNowTime());
hash.put("STATE", STATE);
hash.put("MINRTT", MINRTT.trim());
hash.put("AVGRTT", AVGRTT.trim());
hash.put("MAXRTT", MAXRTT.trim());
} catch (Exception e) {
logger.error("Exception while getHPUnixData:" + result, e);
}
return hash;
}
public static void main(String[] args) {
String ip = args[0];
try {
Map<String, String> map = Ping.ping(ip);
System.out.println("Ping " + ip + ":\t" + map.get("STATE"));
} catch (Exception e) {
e.printStackTrace();
}
}
}