-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPingAction.java
More file actions
64 lines (47 loc) · 1.67 KB
/
PingAction.java
File metadata and controls
64 lines (47 loc) · 1.67 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
package com.appsecco.dvja.controllers;
import org.apache.commons.lang.StringUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PingAction extends BaseController {
private String address;
private String commandOutput;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCommandOutput() {
return commandOutput;
}
public void setCommandOutput(String commandOutput) {
this.commandOutput = commandOutput;
}
public String execute() {
if(StringUtils.isEmpty(getAddress()))
return INPUT;
try {
doExecCommand();
} catch (Exception e) {
addActionMessage("Error running command: " + e.getMessage());
}
return SUCCESS;
}
private void doExecCommand() throws IOException {
Runtime runtime = Runtime.getRuntime();
String[] command = { "/bin/bash", "-c", "ping -t 5 -c 5 " + getAddress() };
Process process = runtime.exec(command);
BufferedReader stdinputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
String output = "Output:\n\n";
while((line = stdinputReader.readLine()) != null)
output += line + "\n";
output += "\n";
output += "Error:\n\n";
stdinputReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while((line = stdinputReader.readLine()) != null)
output += line + "\n";
setCommandOutput(output);
}
}