PingTraceUtil.java
3.25 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
package com.sitech.ismp.util;
import java.util.Date;
import com.adventnet.utils.agent.RunCmd;
/**
* Created by IntelliJ IDEA.
* User: LJ
* Date: 2010-2-23
* Time: 11:09:02
* To change this template use File | Settings | File Templates.
*/
public class PingTraceUtil {
public PingTraceUtil()
{
}
public String getResult(String s, int i)
{
String s1;
if(i == Ping)
s1 = getPingCmd(s);
else
if(i == Trace)
s1 = getTraceCmd(s);
else
throw new IllegalArgumentException("command cannot be equal to " + i + " it must either be 0 or 1");
return execCmd(s1);
}
public String getPingCmd(String s)
{
String s1 = System.getProperty("os.name").toLowerCase();
//System.out.println("#################the system version is "+s1);
String s3 = "ping -n 3";
String s4 = "1";
String s2;
if(s1.indexOf("sunos") != -1 || s1.indexOf("solaris") != -1)
{
s3 = "/usr/sbin/ping -s";
s2 = new String(s3 + " " + s + " 1 " + s4);
} else
if(s1.indexOf("linux") != -1)
{
s3 = "/bin/ping -c 1 -w 1 ";
s2 = new String(s3 + " " + s);
} else
if(s1.indexOf("freebsd") != -1)
{
s3 = "/sbin/ping -c 1";
s2 = new String(s3 + " " + s);
} else
if(s1.indexOf("windows") != -1)
{
s3 = "ping -n 3";
s2 = new String(s3 + " " + s);
}
else
if(s1.indexOf("hp-ux") != -1)
{
s3 = "ping ";
s2 = new String(s3 + " " + s+" -n 3 -m 3");
}
else
{
s2 = new String(s3 + " " + s);
}
return s2;
}
public String getTraceCmd(String s)
{
String s1 = "30";
String s2 = "tracert";
String s3 = s2 + " " + s;
String s4 = System.getProperty("os.name").toLowerCase();
// System.out.println("#################the system version is "+s4);
if(s4.indexOf("linux") != -1)
s3 = "/usr/sbin/traceroute -m " + s1 + " " + s;
else
if(s4.indexOf("freebsd") != -1)
{
s2 = "/sbin/traceroute -c 1";
s3 = new String(s2 + " " + s);
} else
if(s4.indexOf("windows") != -1)
s3 = "tracert -h " + s1 + " " + s;
else
if(s4.indexOf("sunos") != -1 || s4.indexOf("solaris") != -1)
s3 = "/usr/sbin/traceroute -m " + s1 + " " + s;
else
s3 = new String(s2 + " " + s);
return s3;
}
private String execCmd(String s)
{
int i = 60000;
// System.out.println("###################execCmd :"+s);
RunCmd runcmd = new RunCmd(s);
Date date = new Date();
runcmd.start();
while(runcmd.isAlive())
{
try
{
Thread.sleep(100L);
}
catch(Exception exception) { }
Date date1 = new Date();
if(date1.getTime() > date.getTime() + (long)i)
{
runcmd.stopCommand();
return "Timed out after " + i / 1000 + " " + "secs" + ": " + s;
}
}
//System.out.println("#######################PingTraceUtil runcmd.stdout.toString():"+runcmd.stdout.toString());
return runcmd.stdout.toString();
}
public static int Ping = 0;
public static int Trace = 1;
}