-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjPingpingThread.java
More file actions
208 lines (167 loc) · 7.65 KB
/
Copy pathjPingpingThread.java
File metadata and controls
208 lines (167 loc) · 7.65 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright 2013 Adam Lara
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class jPingpingThread extends Thread {
jPingping gui;
public jPingpingThread(jPingping in) {
gui = in;
}
public void run() {
runPing();
}
public void runPing() {
try {
String cmd = "";
String host = "98.130.148.213"; //hostname or IP to ping
String[] results = new String[60];
String[] secondaryResults = new String[120]; //***NEW
String fullResults = "";
String eol = System.getProperty("line.separator");
fullResults = fullResults + "Name: " + gui.nameField.getText() + eol;
fullResults = fullResults + "Email: " + gui.emailField.getText() + eol;
fullResults = fullResults + "IP Address: " + gui.ip + eol;
int loopCount = 0;
int timedoutCount = 0; //***NEW
if(System.getProperty("os.name").startsWith("Windows")) {
// For Windows
cmd = "ping -n 10 " + host;
gui.out_console.append("Windows OS detected, beginning ping test to remote host." + eol);
gui.out_console.append("''" + cmd + "''" + eol);
} else {
// For Linux and OSX
cmd = "ping -c 10 " + host;
gui.out_console.append("Mac OSX or Linux OS detected, beginning ping test to remote host." + eol);
gui.out_console.append(cmd + eol);
}
Process runPing = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(runPing.getInputStream()));
String lineRead = null;
while((lineRead = reader.readLine()) != null)
{
results[loopCount]=lineRead;
if (lineRead.contains("timed out")) {
timedoutCount++;
}
loopCount++;
gui.out_console.append(lineRead + "\n");
}
//***NEW
if (timedoutCount >= 3) {
loopCount = 0;
gui.out_console.append("Abnormal time out errors detected, running secondary test suite...\n");
if(System.getProperty("os.name").startsWith("Windows")) {
// For Windows
cmd = "ipconfig /all ";
gui.out_console.append(eol + "Checking network interfaces." + eol);
gui.out_console.append("''" + cmd + "''" + eol);
} else {
// For Linux and OSX
cmd = "ifconfig -a ";
gui.out_console.append(eol + "Checking network interfaces." + eol);
gui.out_console.append(cmd + eol);
}
runPing = Runtime.getRuntime().exec(cmd);
BufferedReader reader2 = new BufferedReader(new InputStreamReader(runPing.getInputStream()));
String lineRead2 = null;
while((lineRead2 = reader2.readLine()) != null)
{
secondaryResults[loopCount]=lineRead2;
loopCount++;
gui.out_console.append(lineRead2 + "\n");
}
if(System.getProperty("os.name").startsWith("Windows")) {
// For Windows
cmd = "tracert " + host;
gui.out_console.append(eol + "Running trace route to remote host." + eol);
gui.out_console.append("''" + cmd + "''" + eol);
} else {
// For Linux and OSX
cmd = "traceroute " + host;
gui.out_console.append(eol + "Running trace route to remote host." + eol);
gui.out_console.append(cmd + eol);
}
runPing = Runtime.getRuntime().exec(cmd);
BufferedReader reader3 = new BufferedReader(new InputStreamReader(runPing.getInputStream()));
String lineRead3 = null;
while((lineRead3 = reader3.readLine()) != null)
{
secondaryResults[loopCount]=lineRead3;
loopCount++;
gui.out_console.append(lineRead3 + "\n");
}
}//****NEW end.
runPing.waitFor();
if(runPing.exitValue() == 0) {
gui.out_console.append("Test successful!" + "\n");
} else {
gui.out_console.append("Test was not successful, problems encountered." + "\n");
}
Properties props = System.getProperties();
props.put("mail.smtp.host","SMTP-MAIL-HOST");
props.put("mail.smtp.port","SMTP-PORT");
props.put("mail.smtp.auth","true-false"); //smtp authentication
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("email@address.com","password"); //smtp login & password
}
};
int size = results.length;
int size2 = secondaryResults.length;
for (int i=0; i<size; i++)
{
if(results[i]!=null){
fullResults = fullResults + eol + results[i];
}
}
for (int u=0; u<size2; u++)
{
if(secondaryResults[u]!=null){
fullResults = fullResults + eol + secondaryResults[u];
}
}
sendMail(props,auth,new InternetAddress("test@nnovationlabs.com"),"recipient@email.com","Ping Results",fullResults,"text/plain");
}catch(Exception e)
{
e.printStackTrace();
gui.out_console.append(e + "\n");
}
}
public void sendMail(Properties props,
Authenticator authenticator,
InternetAddress fromAddress,
String recipients,
String subject,
String content,
String contentType) {
gui.out_console.append("\nTest completed, results have been emailed. \n");
try {
Session session = Session.getDefaultInstance(props, authenticator);
Message message = new MimeMessage(session);
message.setFrom(fromAddress);
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients, false));
message.setSubject(subject);
message.setContent(content,contentType);
message.setSentDate(new Date());
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}//sendmail
}