gerrit-events/src/main/java/com/sonyericsson/hudson/plugins/gerrit/gerritevents/ssh/SshConnectionImpl.java
/**
        * Execute an ssh command on the server.
        * After the command is sent the used channel is disconnected.
        *
        * @param command the command to execute.
        * @return a String containing the output from the command.
         * @throws SshException if so.
         */
Execute an ssh command on the server.
After the command is sent the used channel is disconnected.

@param command the command to execute.
@return a String containing the output from the command.
@throws SshException if so.
Execute an ssh command on the server . After the command is sent the used channel is disconnected . @param command the command to execute . @return a String containing the output from the command . @throws SshException if so .
@Override
    public synchronized String executeCommand(String command) throws SshException {
        if (!isConnected()) {
            throw new IllegalStateException("Not connected!");
        }
        try {
            logger.debug("Opening channel");
            Channel channel = connectSession.openChannel(CMD_EXEC);
            ((ChannelExec)channel).setCommand(command);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(channel.getInputStream()));
            logger.debug("connecting channel.");
            channel.connect();
            String incomingLine = null;
            StringBuilder commandOutput = new StringBuilder();
            while ((incomingLine = bufferedReader.readLine()) != null) {
                commandOutput.append(incomingLine);
                commandOutput.append('\n');
                logger.trace("Incoming line: {}", incomingLine);
            }
            logger.trace("Closing reader.");
            bufferedReader.close();
            logger.trace("disconnecting channel.");
            channel.disconnect();
            return commandOutput.toString();
        } catch (JSchException ex) {
            throw new SshException(ex);
        } catch (IOException ex) {
            throw new SshException(ex);
        }
    }
@ Override public synchronized String executeCommand ( String command ) throws SshException { if ( ! isConnected ( ) ) { throw new IllegalStateException ( "Not connected!" ) ; } try { logger . debug ( "Opening channel" ) ; Channel channel = connectSession . openChannel ( CMD_EXEC ) ; ( ( ChannelExec ) channel ) . setCommand ( command ) ; BufferedReader bufferedReader = new BufferedReader ( new InputStreamReader ( channel . getInputStream ( ) ) ) ; logger . debug ( "connecting channel." ) ; channel . connect ( ) ; String incomingLine = null ; StringBuilder commandOutput = new StringBuilder ( ) ; while ( ( incomingLine = bufferedReader . readLine ( ) ) != null ) { commandOutput . append ( incomingLine ) ; commandOutput . append ( '\n' ) ; logger . trace ( "Incoming line: {}" , incomingLine ) ; } logger . trace ( "Closing reader." ) ; bufferedReader . close ( ) ; logger . trace ( "disconnecting channel." ) ; channel . disconnect ( ) ; return commandOutput . toString ( ) ; } catch ( JSchException ex ) { throw new SshException ( ex ) ; } catch ( IOException ex ) { throw new SshException ( ex ) ; } }
Execute an ssh command on the server.
Execute an ssh command on the server .
// Seems like Gerrit does not like when you disconnect directly after the command has been sent. // For instance, we have seen effects of mails not being sent out. This is the reason for // receiving all the incoming data.