从jsp

时间:2016-10-07 09:46:43

标签: java jsp jsch

所以我需要通过单击jsp中的按钮来执行unix命令。

这是jsp:

<body>
    <div align="center">
    <h2>Welcome to ETL Automation Testing Tool</h2>
    <h3>Please select Graph:</h3>
    <table border="1">
    <th>Graph</th>

    <tr>
    <td>Graph 1</td>
    <td><INPUT TYPE="radio" NAME="graph" VALUE="radio1"></td>
    </tr>

    </table>
    <form action="${pageContext.request.contextPath}/myservlet" method="post">
    <input type="submit" value="Submit" name="submit" />
    </form>
    </div>
</body>

那里非常简单。

这是servlet:

@WebServlet("/myservlet")
public class MyServlet extends HttpServlet {

    @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            UnixPrompt unixprompt = new UnixPrompt();
            unixprompt.connect();
        }       
    }

这是连接到ssh并执行命令的代码:

import com.jcraft.jsch.*;

import java.io.*;

public class UnixPrompt{
  public static void connect(){
    try{
      JSch jsch=new JSch();  

      String host=null;

      String user="******";
      host="*******";
      String config =
                "Port 22\n"+
                "\n"+
                "Host foo\n"+
                "  User "+user+"\n"+
                "  Hostname "+host+"\n"+
                "Host *\n"+
                "  ConnectTime 30000\n"+
                "  PreferredAuthentications keyboard-interactive,password,publickey\n"+
                "  #ForwardAgent yes\n"+
                "  #StrictHostKeyChecking no\n"+
                "  #IdentityFile ~/.ssh/id_rsa\n"+
                "  #UserKnownHostsFile ~/.ssh/known_hosts";



              ConfigRepository configRepository =
                com.jcraft.jsch.OpenSSHConfig.parse(config);


              jsch.setConfigRepository(configRepository);
              Session session=jsch.getSession("foo");


      String passwd ="*****";
      session.setPassword(passwd);
      UserInfo ui = new MyUserInfo(){

          public boolean promptYesNo(String message){

            int foo = 0;
            return foo==0;
          }
      };
      session.setUserInfo(ui);
      session.connect();

      String command="pwd";

      Channel channel=session.openChannel("exec");
      ((ChannelExec)channel).setCommand(command);


      channel.setInputStream(null);


      ((ChannelExec)channel).setErrStream(System.err);

      InputStream in=channel.getInputStream();

      channel.connect();

      byte[] tmp=new byte[1024];
      while(true){
        while(in.available()>0){
          int i=in.read(tmp, 0, 1024);
          if(i<0)break;
          System.out.print(new String(tmp, 0, i));
        }
        if(channel.isClosed()){
          if(in.available()>0) continue; 
          System.out.println("exit-status: "+channel.getExitStatus());
          break;
        }
        try{Thread.sleep(1000);}catch(Exception ee){}
      }

      channel.disconnect();
      session.disconnect();
    }
    catch(Exception e){
      System.out.println(e);
    }


  }
  public static void main(String arg[]){
    connect();
  }

  public static abstract class MyUserInfo
    implements UserInfo, UIKeyboardInteractive{
public String getPassword(){ return null; }
public boolean promptYesNo(String str){ return false; }
public String getPassphrase(){ return null; }
public boolean promptPassphrase(String message){ return false; }
public boolean promptPassword(String message){ return false; }
public void showMessage(String message){ }
public String[] promptKeyboardInteractive(String destination,
                        String name,
                        String instruction,
                        String[] prompt,
                        boolean[] echo){
return null;
}
}
}

我可以通过独立的UnixPrompt类连接到SSH并执行命令,但是无法通过jsp执行此操作。

这是我得到的错误:

  

SEVERE:servlet [unix.MyServlet]的Servlet.service()与上下文   路径[/ Scratchpad]抛出异常[Servlet执行扔了一个   异常]与根本原因java.lang.ClassNotFoundException:   com.jcraft.jsch.ConfigRepository at   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1858)     在   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1709)     在unix.MyServlet.doPost(MyServlet.java:17)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:650)at at   javax.servlet.http.HttpServlet.service(HttpServlet.java:731)at at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)     在   org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)     在   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:625)     在   org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:318)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(Thread.java:722)

错误引用的类存在于版本0.1.54的较新的JSch jar中,我已将其包含在构建路径中,所以我不确定问题是什么。

任何帮助将不胜感激

0 个答案:

没有答案