package gov.jdaccs.comm; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import com.jcraft.jsch.*; /** * SecureShellChannel is a class that creates a Java Secure Channel using the * JCraft JSch library. * * @author Jonathon Sanders * @version 2.0.0 */ public abstract class SecureShellChannel { protected static final int PORT = 22; protected static final int TIMEOUT_MILLISECONDS = 60000; protected String host; protected Properties config; protected JSch ssh; protected Session session; protected Channel channel; /** * Creates a SecureShellChannel to connect to the specified host. * * @param host * the host to connect to through the secure channel. */ public SecureShellChannel(String host) { this.host = host; config = new Properties(); // Example says this should be for testing only. config.put("StrictHostKeyChecking", "no"); ssh = new JSch(); } /** * Application specific way to prompt user for credentials to create and log * into SSH session. */ public abstract void promptUser(); /** * Creates the SSH session. * * @param username * username. * @param password * password. * @throws JSchException */ public void createSession(String username, char[] password) throws JSchException { String key = ""; for (int n = 0; n < password.length; n++) { key += password[n]; } session = ssh.getSession(username, host, PORT); session.setPassword(key); session.setConfig(config); // Force Garbage collector to run to get rid of password. System.gc(); } /** * Connects to the SSH session. * * @throws JSchException */ public void connect() throws JSchException { // Check if Session exists. if (session == null) throw new NullPointerException( "Session is null, call createSession(username, password)!"); session.connect(TIMEOUT_MILLISECONDS); } /** * Returns if the Session is connected. * * @return true if connected, false otherwise. */ public boolean isConnected() { // Check if Session exists. if (session == null) return false; return session.isConnected(); } /** * Sends commands to the host through a secure channel. * * @param command * the command to send. * @return the output returned from the command. * @throws IOException * @throws JSchException * @throws NullPointerException */ public String sendCommand(String command) throws IOException, JSchException, NullPointerException { // Check if Session exists if (session == null) throw new NullPointerException( "Session is null, call createSession(username, password), then connect()!"); if (!session.isConnected()) { System.out.println("connect again"); session.connect(TIMEOUT_MILLISECONDS); } System.out.println("String Builder"); StringBuilder output = new StringBuilder(); System.out.println("Create Channel"); channel = session.openChannel("exec"); System.out.println("Sending command: " + command); ((ChannelExec) channel).setCommand(command); InputStream commandOutput = channel.getInputStream(); channel.connect(); try { Thread.sleep(500); } catch (InterruptedException ie) { ie.printStackTrace(); } int readByte = commandOutput.read(); while (readByte != 0xffffffff) { output.append((char) readByte); readByte = commandOutput.read(); } // close Channel channel.disconnect(); // close Session session.disconnect(); return output.toString(); } /** * Closes the SSH session. */ public void shutdown() { // close Session if (session != null) { // close Session session.disconnect(); } } }