package lucee.runtime.net.ftp; import java.io.IOException; import java.io.InputStream; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; public class SSHManager { private JSch jschSSHChannel; private String strUserName; private String strConnectionIP; private int intConnectionPort; private String strPassword; private Session sesConnection; private int intTimeOut; private void doCommonConstructorActions(String userName, String password, String connectionIP, String knownHostsFileName) throws JSchException { jschSSHChannel = new JSch(); jschSSHChannel.setKnownHosts(knownHostsFileName); strUserName = userName; strPassword = password; strConnectionIP = connectionIP; } public SSHManager(String userName, String password, String connectionIP, String knownHostsFileName) throws JSchException { doCommonConstructorActions(userName, password, connectionIP, knownHostsFileName); intConnectionPort = 22; intTimeOut = 60000; } public SSHManager(String userName, String password, String connectionIP, String knownHostsFileName, int connectionPort) throws JSchException { doCommonConstructorActions(userName, password, connectionIP, knownHostsFileName); intConnectionPort = connectionPort; intTimeOut = 60000; } public SSHManager(String userName, String password, String connectionIP, String knownHostsFileName, int connectionPort, int timeOutMilliseconds) throws JSchException { doCommonConstructorActions(userName, password, connectionIP, knownHostsFileName); intConnectionPort = connectionPort; intTimeOut = timeOutMilliseconds; } public String connect() { String errorMessage = null; try { sesConnection = jschSSHChannel.getSession(strUserName, strConnectionIP, intConnectionPort); sesConnection.setPassword(strPassword); // UNCOMMENT THIS FOR TESTING PURPOSES, BUT DO NOT USE IN PRODUCTION // sesConnection.setConfig("StrictHostKeyChecking", "no"); sesConnection.connect(intTimeOut); } catch(JSchException jschX) { errorMessage = jschX.getMessage(); } return errorMessage; } public String sendCommand(String command) throws JSchException, IOException { StringBuilder outputBuffer = new StringBuilder(); Channel channel = sesConnection.openChannel("exec"); ((ChannelExec)channel).setCommand(command); InputStream commandOutput = channel.getInputStream(); channel.connect(); int readByte = commandOutput.read(); while(readByte != 0xffffffff) { outputBuffer.append((char)readByte); readByte = commandOutput.read(); } channel.disconnect(); return outputBuffer.toString(); } public void close() { sesConnection.disconnect(); } }