Search in sources :

Example 11 with StreamGobbler

use of com.trilead.ssh2.StreamGobbler in project maven-scm by apache.

the class ExtConnection method open.

/**
 * {@inheritDoc}
 */
public void open() throws AuthenticationException, CommandAbortedException {
    connection = new Connection(host, port);
    try {
        // TODO: connection timeout?
        connection.connect();
    } catch (IOException e) {
        String message = "Cannot connect. Reason: " + e.getMessage();
        throw new AuthenticationException(message, e, message);
    }
    File privateKey = getPrivateKey();
    try {
        boolean authenticated;
        if (privateKey != null && privateKey.exists()) {
            authenticated = connection.authenticateWithPublicKey(userName, privateKey, getPassphrase());
        } else {
            authenticated = connection.authenticateWithPassword(userName, password);
        }
        if (!authenticated) {
            String message = "Authentication failed.";
            throw new AuthenticationException(message, message);
        }
    } catch (IOException e) {
        closeConnection();
        String message = "Cannot authenticate. Reason: " + e.getMessage();
        throw new AuthenticationException(message, e, message);
    }
    try {
        session = connection.openSession();
    } catch (IOException e) {
        String message = "Cannot open session. Reason: " + e.getMessage();
        throw new CommandAbortedException(message, message);
    }
    String command = "cvs server";
    try {
        session.execCommand(command);
    } catch (IOException e) {
        String message = "Cannot execute remote command: " + command;
        throw new CommandAbortedException(message, message);
    }
    InputStream stdout = new StreamGobbler(session.getStdout());
    InputStream stderr = new StreamGobbler(session.getStderr());
    stderrReader = new BufferedReader(new InputStreamReader(stderr));
    setInputStream(new LoggedDataInputStream(stdout));
    setOutputStream(new LoggedDataOutputStream(session.getStdin()));
}
Also used : StreamGobbler(ch.ethz.ssh2.StreamGobbler) CommandAbortedException(org.netbeans.lib.cvsclient.command.CommandAbortedException) InputStreamReader(java.io.InputStreamReader) AuthenticationException(org.netbeans.lib.cvsclient.connection.AuthenticationException) LoggedDataInputStream(org.netbeans.lib.cvsclient.util.LoggedDataInputStream) InputStream(java.io.InputStream) Connection(ch.ethz.ssh2.Connection) AbstractConnection(org.netbeans.lib.cvsclient.connection.AbstractConnection) IOException(java.io.IOException) LoggedDataOutputStream(org.netbeans.lib.cvsclient.util.LoggedDataOutputStream) BufferedReader(java.io.BufferedReader) File(java.io.File) LoggedDataInputStream(org.netbeans.lib.cvsclient.util.LoggedDataInputStream)

Example 12 with StreamGobbler

use of com.trilead.ssh2.StreamGobbler in project new-cloud by xie-summer.

the class SSHTemplate method processStream.

/**
 * 从流中获取内容
 * @param is
 */
private void processStream(InputStream is, LineProcessor lineProcessor) {
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(new StreamGobbler(is)));
        String line = null;
        int lineNum = 1;
        while ((line = reader.readLine()) != null) {
            try {
                lineProcessor.process(line, lineNum);
            } catch (Exception e) {
                logger.error("err line:" + line, e);
            }
            lineNum++;
        }
        lineProcessor.finish();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        close(reader);
    }
}
Also used : StreamGobbler(ch.ethz.ssh2.StreamGobbler) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) SSHException(com.sohu.cache.exception.SSHException)

Example 13 with StreamGobbler

use of com.trilead.ssh2.StreamGobbler in project warn-report by saaavsaaa.

the class LinuxExecUtil method execute.

public void execute(final String commandText) {
    try {
        System.out.println("exec : " + commandText);
        Session sess = conn.openSession();
        sess.execCommand(commandText);
        System.out.println("Here is some information about the remote host:");
        InputStream standardOut = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(standardOut));
        while (true) {
            String line = br.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
        }
        System.out.println("ExitCode: " + sess.getExitStatus());
        sess.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : StreamGobbler(ch.ethz.ssh2.StreamGobbler) Session(ch.ethz.ssh2.Session)

Aggregations

StreamGobbler (ch.ethz.ssh2.StreamGobbler)7 IOException (java.io.IOException)7 StreamGobbler (com.trilead.ssh2.StreamGobbler)6 BufferedReader (java.io.BufferedReader)6 InputStream (java.io.InputStream)6 InputStreamReader (java.io.InputStreamReader)6 Connection (com.trilead.ssh2.Connection)5 Session (com.trilead.ssh2.Session)5 Session (ch.ethz.ssh2.Session)4 SSHException (com.sohu.cache.exception.SSHException)2 OutputStream (java.io.OutputStream)2 TimeoutException (java.util.concurrent.TimeoutException)2 Connection (ch.ethz.ssh2.Connection)1 HTTPProxyData (com.trilead.ssh2.HTTPProxyData)1 ServiceException (com.zimbra.common.service.ServiceException)1 File (java.io.File)1 CommandAbortedException (org.netbeans.lib.cvsclient.command.CommandAbortedException)1 AbstractConnection (org.netbeans.lib.cvsclient.connection.AbstractConnection)1 AuthenticationException (org.netbeans.lib.cvsclient.connection.AuthenticationException)1 LoggedDataInputStream (org.netbeans.lib.cvsclient.util.LoggedDataInputStream)1