001package lucee.runtime.net.ftp;
002
003import java.io.IOException;
004import java.io.InputStream;
005
006import com.jcraft.jsch.*;
007
008public class SSHManager {
009  private JSch jschSSHChannel;
010  private String strUserName;
011  private String strConnectionIP;
012  private int intConnectionPort;
013  private String strPassword;
014  private Session sesConnection;
015  private int intTimeOut;
016
017  private void doCommonConstructorActions(String userName, 
018       String password, String connectionIP, String knownHostsFileName) throws JSchException {
019     jschSSHChannel = new JSch();
020     jschSSHChannel.setKnownHosts(knownHostsFileName);
021     
022
023     strUserName = userName;
024     strPassword = password;
025     strConnectionIP = connectionIP;
026  }
027
028  public SSHManager(String userName, String password, 
029     String connectionIP, String knownHostsFileName) throws JSchException
030  {
031     doCommonConstructorActions(userName, password, 
032                connectionIP, knownHostsFileName);
033     intConnectionPort = 22;
034     intTimeOut = 60000;
035  }
036
037  public SSHManager(String userName, String password, String connectionIP, 
038     String knownHostsFileName, int connectionPort) throws JSchException
039  {
040     doCommonConstructorActions(userName, password, connectionIP, 
041        knownHostsFileName);
042     intConnectionPort = connectionPort;
043     intTimeOut = 60000;
044  }
045
046  public SSHManager(String userName, String password, String connectionIP, 
047      String knownHostsFileName, int connectionPort, int timeOutMilliseconds) throws JSchException {
048     doCommonConstructorActions(userName, password, connectionIP, 
049         knownHostsFileName);
050     intConnectionPort = connectionPort;
051     intTimeOut = timeOutMilliseconds;
052  }
053
054  public String connect()
055  {
056     String errorMessage = null;
057
058     try
059     {
060        sesConnection = jschSSHChannel.getSession(strUserName, 
061            strConnectionIP, intConnectionPort);
062        sesConnection.setPassword(strPassword);
063        // UNCOMMENT THIS FOR TESTING PURPOSES, BUT DO NOT USE IN PRODUCTION
064        // sesConnection.setConfig("StrictHostKeyChecking", "no");
065        sesConnection.connect(intTimeOut);
066     }
067     catch(JSchException jschX)
068     {
069        errorMessage = jschX.getMessage();
070     }
071
072     return errorMessage;
073  }
074
075
076
077  public String sendCommand(String command) throws JSchException, IOException {
078     StringBuilder outputBuffer = new StringBuilder();
079
080   
081        Channel channel = sesConnection.openChannel("exec");
082        ((ChannelExec)channel).setCommand(command);
083        InputStream commandOutput = channel.getInputStream();
084        channel.connect();
085        int readByte = commandOutput.read();
086
087        while(readByte != 0xffffffff)
088        {
089           outputBuffer.append((char)readByte);
090           readByte = commandOutput.read();
091        }
092
093        channel.disconnect();
094     
095
096     return outputBuffer.toString();
097  }
098
099  public void close()
100  {
101     sesConnection.disconnect();
102  }
103
104  }