My ExecuteScript.java class
package scriptOnJsp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
//@WebServlet("/ExecuteScript")
public class ExecuteScript extends HttpServlet {
private static final long serialVersionUID = 1L;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
HashMap<String, String> hashMap = new HashMap<String, String>();
HashMap<String, HashMap<String, String>> hashingMap = new HashMap<String,
HashMap<String, String>>();
MapValue keyValue = new MapValue();
String scriptResult;
String host;
String password;
String command = "ls";
String executeDirectory = "/t3/envs/wlppp4/";
public ExecuteScript() {
super();
}
/*
* protected void doGet(HttpServletRequest request, HttpServletResponse
* response) throws ServletException, IOException {
* response.setContentType("text/html"); PrintWriter printWriter =
* response.getWriter(); printWriter.println("<h1>ExecuteScript</h1>"); }
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
// Get user_name and pass_word from the JSP page
host = request.getParameter("host_name");
String password = request.getParameter("pass_word"); // Print the
above
// got values in
// console
System.out.println("The username is" + host);
System.out.println("\nand the password is" + password);
request.setAttribute("dd", keyValue);
}
public void connect() {
try {
JSch jsch = new JSch();
session = jsch.getSession("sftp_t3", host, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
java.util.Properties config = new java.util.Properties();
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd(executeDirectory);
executeCommand(command);
System.out.println("Executed :)");
} catch (Exception e) {
e.printStackTrace();
}
}
public void executeCommand(String script) throws JSchException,
IOException, SftpException {
System.out.println("Execute sudo");
String sudo_pass = "test";
ChannelExec channel = (ChannelExec) session.openChannel("exec");
Channel channelTemp = (ChannelSftp) session.openChannel("sftp");
((ChannelExec) channel).setCommand(script);
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
out.write((sudo_pass + "\n").getBytes());
out.flush();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
scriptResult = new String(tmp, 0, i);
String[] strArray = scriptResult.split("\n");
for (String temp : strArray) {
int var = temp.indexOf(":");
String arr = temp.substring(0, var);
String arry = temp.substring(var,
temp.length());
hashMap.put(arr, arry);
}
for (Map.Entry<String, String> pairs :
hashMap.entrySet()) {
System.out.println("" + pairs.getKey() +
pairs.getValue());
}
keyValue.setMinDays(hashMap.get("Last password
change"));
keyValue.setPassExpires(hashMap.get("Password
expires"));
keyValue.setPassInactive(hashMap.get("Password
inactive"));
keyValue.setAccExpires(hashMap.get("Account
expires"));
keyValue.setMinDays(hashMap
.get("Minimum number of days between
password change"));
keyValue.setMaxDays(hashMap
.get("Maximum number of days between
password change"));
keyValue.setNoWarnings(hashMap
.get("Number of days of warning
before password expires"));
hashingMap.put(host, hashMap);
for (Map.Entry<String, HashMap<String, String>>
newPairs : hashingMap
.entrySet()) {
System.out.println("Set 1: \n" +
newPairs.getKey()
+ newPairs.getValue());
for (Map.Entry<String, String> newPairsTemp
: newPairs
.getValue().entrySet()) {
System.out.println("Set 2: \n" +
newPairsTemp.getKey()
+
newPairsTemp.getValue());
}
}
System.out.print("Data are: " + scriptResult);
}
if (channel.isClosed()) {
System.out.println("exit-status: " +
channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
System.out.println(ee);
}
}
channel.disconnect();
System.out.println("Sudo disconnect");
}
public void disconnect() {
session.disconnect();
}
public void Openpage(HttpServletResponse res) throws IOException {
// here type your JSP page that you want to open
res.sendRedirect("host.jsp");
}
/*
* public static void main(String... args) throws JSchException,
* IOException, SftpException {
*
* ExecuteScript escript = new ExecuteScript();
*
* //escript.processRequest(request, response); escript.connect();
* escript.disconnect(); }
*/
}
My bean class -- MapValue.java
package scriptOnJsp;
public class MapValue {
private String minDays;
private String passInactive;
private String lastPassChange;
private String maxDays;
private String passExpires;
private String accExpires;
private String noWarnings;
public String getMinDays() {
return minDays;
}
public void setMinDays(String minDays) {
this.minDays = minDays;
}
public String getPassInactive() {
return passInactive;
}
public void setPassInactive(String passInactive) {
this.passInactive = passInactive;
}
public String getLastPassChange() {
return lastPassChange;
}
public void setLastPassChange(String lastPassChange) {
this.lastPassChange = lastPassChange;
}
public String getMaxDays() {
return maxDays;
}
public void setMaxDays(String maxDays) {
this.maxDays = maxDays;
}
public String getPassExpires() {
return passExpires;
}
public void setPassExpires(String passExpires) {
this.passExpires = passExpires;
}
public String getAccExpires() {
return accExpires;
}
public void setAccExpires(String accExpires) {
this.accExpires = accExpires;
}
public String getNoWarnings() {
return noWarnings;
}
public void setNoWarnings(String noWarnings) {
this.noWarnings = noWarnings;
}
}
My jsp -- host.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Host details</title>
</head>
<body>
<form name=host action="SaveUser" method="Post">
Host name: <input type="text" name="host_name"> <br />
Password: <input type="password" name="pass_word" /> <input
type="submit" value="Submit" />
</form>
</body>
</html>
> My web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Scriptingpage</display-name>
<welcome-file-list>
<welcome-file>host.jsp</welcome-file>
</welcome-file-list>
</web-app>
I need to fetch the user entery on host name and fetch the same into java class, manipulate it and display the ezxecuted query as a key value pair. In this, I'm not able to even display my jsp page. Kindly help me out of this.
If you getting 404 error that means the URL you put on the address bar is not existing. before checking into your codes, please make sure you have uploaded your files to the correct location of the server and you have given the correct path ti the browser.
You need to add the entry for your servlet in web.xml
as below,
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Scriptingpage</display-name>
<servlet>
<servlet-name>ExecuteScript</servlet-name>
<servlet-class>PackageName.ExecuteScript</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExecuteScript</servlet-name>
<url-pattern>/ExecuteScript</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>host.jsp</welcome-file>
</welcome-file-list>
</web-app>
And in your jsp ,
<form name=host action="/ExecuteScript" method="Post">
Host name: <input type="text" name="host_name"> <br />
Password: <input type="password" name="pass_word" /> <input
type="submit" value="Submit" />
</form>
url in action should match the url pattern provide in the web.xml
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.