Class SSHLauncher


  • @Service(name="SSHLauncher")
    @PerLookup
    public class SSHLauncher
    extends Object
    Author:
    Rajiv Mordani
    • Constructor Detail

      • SSHLauncher

        public SSHLauncher()
    • Method Detail

      • init

        public void init​(Logger logger)
      • init

        public void init​(Node node,
                         Logger logger)
        Initialize the SSHLauncher use a Node config object
        Parameters:
        node -
        logger -
      • init

        public void init​(String userName,
                         String host,
                         int port,
                         String password,
                         char[] privateKey,
                         Logger logger)
        Initialize the SSHLauncher using a private key
        Parameters:
        userName -
        host -
        port -
        password -
        privateKey -
        logger -
      • init

        public void init​(String userName,
                         String host,
                         int port,
                         String password,
                         String keyFile,
                         String keyPassPhrase,
                         Logger logger)
        Initialize the SSHLauncher using a private key file
        Parameters:
        userName -
        host -
        port -
        password -
        keyFile -
        keyPassPhrase -
        logger -
      • runCommand

        public int runCommand​(List<String> command,
                              OutputStream os,
                              List<String> stdinLines)
                       throws IOException,
                              InterruptedException
        Executes a command on the remote system via ssh, optionally sending lines of data to the remote process's System.in.
        Parameters:
        command - the command to execute in the form of an argv style list
        os - stream to receive the output from the command
        stdinLines - optional data to be sent to the process's System.in stream; null if no input should be sent
        Returns:
        Throws:
        IOException
        InterruptedException
      • runCommand

        public int runCommand​(String command,
                              OutputStream os,
                              List<String> stdinLines)
                       throws IOException,
                              InterruptedException
        Executes a command on the remote system via ssh, optionally sending lines of data to the remote process's System.in. WARNING! This method does not handle paths with spaces in them. To use this method you must make sure all paths in the command string are quoted correctly. Otherwise use the methods that take command as a list instead.
        Parameters:
        command - the command to execute
        os - stream to receive the output from the command
        stdinLines - optional data to be sent to the process's System.in stream; null if no input should be sent
        Returns:
        Throws:
        IOException
        InterruptedException
      • runCommandAsIs

        public int runCommandAsIs​(List<String> command,
                                  OutputStream os,
                                  List<String> stdinLines)
                           throws IOException,
                                  InterruptedException
        Executes a command on the remote system via ssh without normalizing the command line
        Parameters:
        command - the command to execute
        os - stream to receive the output from the command
        stdinLines - optional data to be sent to the process's System.in stream; null if no input should be sent
        Returns:
        Throws:
        IOException
        InterruptedException
      • runCommandAsIs

        public int runCommandAsIs​(List<String> command,
                                  OutputStream os,
                                  List<String> stdinLines,
                                  String[] env)
                           throws IOException,
                                  InterruptedException
        Executes a command on the remote system via ssh without normalizing the command line
        Parameters:
        command - the command to execute
        os - stream to receive the output from the command
        stdinLines - optional data to be sent to the process's System.in stream; null if no input should be sent
        env - list of environment variables to set before executing the command. each array cell is like varname=varvalue. This only supports on csh, t-csh and bash
        Returns:
        Throws:
        IOException
        InterruptedException
      • expandPasswordAlias

        public String expandPasswordAlias​(String alias)
      • isPasswordAlias

        public boolean isPasswordAlias​(String alias)
      • setupKey

        public void setupKey​(String node,
                             String pubKeyFile,
                             boolean generateKey,
                             String passwd)
                      throws IOException,
                             InterruptedException
        Setting up the key involves the following steps: -If a key exists and we can connect using the key, do nothing. -Generate a key pair if there isn't one -Connect to remote host using password auth and do the following: 1. create .ssh directory if it doesn't exist 2. copy over the key as key.tmp 3. Append the key to authorized_keys file 4. Remove the temporary key file key.tmp 5. Fix permissions for home, .ssh and authorized_keys
        Parameters:
        node - - remote host
        pubKeyFile - - .pub file
        generateKey - - flag to indicate if key needs to be generated or not
        passwd - - ssh user password
        Throws:
        IOException
        InterruptedException
      • checkConnection

        public boolean checkConnection()
        Check if we can authenticate using public key auth
        Returns:
        true|false
      • checkPasswordAuth

        public boolean checkPasswordAuth()
        Check if we can connect using password auth
        Returns:
        true|false