SSHManager.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.remote.tunnel;
00002 
00003 
00004 import java.io.IOException;
00005 import java.io.InputStream;
00006 import java.util.logging.Level;
00007 import java.util.logging.Logger;
00008 
00009 import com.jcraft.jsch.Channel;
00010 import com.jcraft.jsch.ChannelExec;
00011 import com.jcraft.jsch.JSch;
00012 import com.jcraft.jsch.JSchException;
00013 import com.jcraft.jsch.Session;
00014 
00015 /*****************************************************************
00016  * 
00017  * wrapper class for JSCH library
00018  *
00019  *****************************************************************/
00020 public class SSHManager 
00021 {
00022     private static final Logger LOGGER = 
00023             Logger.getLogger(SSHManager.class.getName());
00024     private JSch jschSSHChannel;
00025     private String strUserName;
00026     private String strConnectionIP;
00027     private int intConnectionPort;
00028     private String strPassword;
00029     private Session sesConnection;
00030     private int intTimeOut;
00031 
00032     private void doCommonConstructorActions(String userName, 
00033             String password, String connectionIP, String knownHostsFileName)
00034     {
00035         jschSSHChannel = new JSch();
00036 
00037         try
00038         {
00039             jschSSHChannel.setKnownHosts(knownHostsFileName);
00040         }
00041         catch(JSchException jschX)
00042         {
00043             logError(jschX.getMessage());
00044         }
00045 
00046         strUserName = userName;
00047         strPassword = password;
00048         strConnectionIP = connectionIP;
00049     }
00050 
00051     public SSHManager(String userName, String password, 
00052             String connectionIP, String knownHostsFileName)
00053     {
00054         doCommonConstructorActions(userName, password, 
00055                 connectionIP, knownHostsFileName);
00056         intConnectionPort = 22;
00057         intTimeOut = 60000;
00058     }
00059 
00060     public SSHManager(String userName, String password, String connectionIP, 
00061             String knownHostsFileName, int connectionPort)
00062     {
00063         doCommonConstructorActions(userName, password, connectionIP, 
00064                 knownHostsFileName);
00065         intConnectionPort = connectionPort;
00066         intTimeOut = 60000;
00067     }
00068 
00069     public SSHManager(String userName, String password, String connectionIP, 
00070             String knownHostsFileName, int connectionPort, int timeOutMilliseconds)
00071     {
00072         doCommonConstructorActions(userName, password, connectionIP, 
00073                 knownHostsFileName);
00074         intConnectionPort = connectionPort;
00075         intTimeOut = timeOutMilliseconds;
00076     }
00077 
00078     public String connect()
00079     {
00080         String errorMessage = null;
00081 
00082         try
00083         {
00084             sesConnection = jschSSHChannel.getSession(strUserName, 
00085                     strConnectionIP, intConnectionPort);
00086             sesConnection.setPassword(strPassword);
00087             // UNCOMMENT THIS FOR TESTING PURPOSES, BUT DO NOT USE IN PRODUCTION
00088             // sesConnection.setConfig("StrictHostKeyChecking", "no");
00089             sesConnection.connect(intTimeOut);
00090         }
00091         catch(JSchException jschX)
00092         {
00093             errorMessage = jschX.getMessage();
00094         }
00095 
00096         return errorMessage;
00097     }
00098 
00099     private String logError(String errorMessage)
00100     {
00101         if(errorMessage != null)
00102         {
00103             LOGGER.log(Level.SEVERE, "{0}:{1} - {2}", 
00104                     new Object[]{strConnectionIP, intConnectionPort, errorMessage});
00105         }
00106 
00107         return errorMessage;
00108     }
00109 
00110     private String logWarning(String warnMessage)
00111     {
00112         if(warnMessage != null)
00113         {
00114             LOGGER.log(Level.WARNING, "{0}:{1} - {2}", 
00115                     new Object[]{strConnectionIP, intConnectionPort, warnMessage});
00116         }
00117 
00118         return warnMessage;
00119     }
00120 
00121     public String sendCommand(String command)
00122     {
00123         StringBuilder outputBuffer = new StringBuilder();
00124 
00125         try
00126         {
00127             Channel channel = sesConnection.openChannel("exec");
00128             ((ChannelExec)channel).setCommand(command);
00129             channel.connect();
00130             InputStream commandOutput = channel.getInputStream();
00131             int readByte = commandOutput.read();
00132 
00133             while(readByte != 0xffffffff)
00134             {
00135                 outputBuffer.append((char)readByte);
00136                 readByte = commandOutput.read();
00137             }
00138 
00139             channel.disconnect();
00140         }
00141         catch(IOException ioX)
00142         {
00143             logWarning(ioX.getMessage());
00144             return null;
00145         }
00146         catch(JSchException jschX)
00147         {
00148             logWarning(jschX.getMessage());
00149             return null;
00150         }
00151 
00152         return outputBuffer.toString();
00153     }
00154 
00155     public void close()
00156     {
00157         sesConnection.disconnect();
00158     }
00159 
00160 
00161     static public void main(String []args)
00162     {
00163         if (args.length != 4) {
00164             System.out.println("Syntax: java SSHManager username password hostname command");
00165             return;
00166         }
00167 
00168         String command = args[3];
00169         String userName = args[0];
00170         String password = args[1];
00171         String connectionIP = args[2];
00172 
00173         System.out.println("sendCommand for: " + userName + "@" + connectionIP + "\npassword: " + password +"\nCommand: " + command);
00174 
00175         SSHManager instance = new SSHManager(userName, password, connectionIP, "");
00176         String errorMessage = instance.connect();
00177 
00178         if(errorMessage != null)
00179         {
00180             System.out.println(errorMessage);
00181         }
00182 
00183         // call sendCommand for each command and the output 
00184         //(without prompts) is returned
00185         String result = instance.sendCommand(command);
00186         // close only after all commands are sent
00187         instance.close();
00188         assert(result != null && result.length()>0);
00189     }
00190 
00191 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1