Search in sources :

Example 1 with StreamGobbler

use of com.trilead.ssh2.StreamGobbler in project intellij-community by JetBrains.

the class BasicWithHTTPProxy method main.

public static void main(String[] args) {
    String hostname = "my-ssh-server";
    String username = "joe";
    String password = "joespass";
    String proxyHost = "192.168.1.1";
    // default port used by squid
    int proxyPort = 3128;
    try {
        /* Create a connection instance */
        Connection conn = new Connection(hostname);
        /* We want to connect through a HTTP proxy */
        conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort));
        // if the proxy requires basic authentication:
        // conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort, "username", "secret"));
        /* Now connect (through the proxy) */
        conn.connect();
        /* Authenticate.
			 * If you get an IOException saying something like
			 * "Authentication method password not supported by the server at this stage."
			 * then please check the FAQ.
			 */
        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("uname -a && date && uptime && who");
        System.out.println("Here is some information about the remote host:");
        /* 
			 * This basic example does not handle stderr, which is sometimes dangerous
			 * (please read the FAQ).
			 */
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        while (true) {
            String line = br.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        /* Show exit status, if available (otherwise "null") */
        System.out.println("ExitCode: " + sess.getExitStatus());
        /* Close this session */
        sess.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : StreamGobbler(com.trilead.ssh2.StreamGobbler) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) HTTPProxyData(com.trilead.ssh2.HTTPProxyData) Session(com.trilead.ssh2.Session)

Example 2 with StreamGobbler

use of com.trilead.ssh2.StreamGobbler in project intellij-community by JetBrains.

the class StdoutAndStderr method main.

public static void main(String[] args) {
    String hostname = "127.0.0.1";
    String username = "joe";
    String password = "joespass";
    try {
        /* Create a connection instance */
        Connection conn = new Connection(hostname);
        /* Now connect */
        conn.connect();
        /* Authenticate */
        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("echo \"Text on STDOUT\"; echo \"Text on STDERR\" >&2");
        InputStream stdout = new StreamGobbler(sess.getStdout());
        InputStream stderr = new StreamGobbler(sess.getStderr());
        BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
        BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
        System.out.println("Here is the output from stdout:");
        while (true) {
            String line = stdoutReader.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        System.out.println("Here is the output from stderr:");
        while (true) {
            String line = stderrReader.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        /* Close this session */
        sess.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : StreamGobbler(com.trilead.ssh2.StreamGobbler) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Session(com.trilead.ssh2.Session)

Example 3 with StreamGobbler

use of com.trilead.ssh2.StreamGobbler in project cachecloud by sohutv.

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 4 with StreamGobbler

use of com.trilead.ssh2.StreamGobbler in project zm-mailbox by Zimbra.

the class RemoteManager method executeBackground0.

private synchronized void executeBackground0(String command, RemoteBackgroundHandler handler) {
    Session s = null;
    try {
        s = getSession();
        if (ZimbraLog.rmgmt.isDebugEnabled())
            ZimbraLog.rmgmt.debug("(bg) executing shim command  '" + mShimCommand + "' on " + this);
        s.execCommand(mShimCommand);
        OutputStream os = s.getStdin();
        String send = "HOST:" + mHost + " " + command;
        if (ZimbraLog.rmgmt.isDebugEnabled())
            ZimbraLog.rmgmt.debug("(bg) sending mgmt command '" + send + "' on " + this);
        os.write(send.getBytes());
        os.close();
        InputStream stdout = new StreamGobbler(s.getStdout());
        InputStream stderr = new StreamGobbler(s.getStderr());
        handler.read(stdout, stderr);
    } catch (OutOfMemoryError e) {
        Zimbra.halt("out of memory", e);
    } catch (Throwable t) {
        handler.error(t);
    } finally {
        if (s != null) {
            releaseSession(s);
        }
    }
}
Also used : StreamGobbler(ch.ethz.ssh2.StreamGobbler) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Session(ch.ethz.ssh2.Session)

Example 5 with StreamGobbler

use of com.trilead.ssh2.StreamGobbler in project zm-mailbox by Zimbra.

the class RemoteManager method execute.

public synchronized RemoteResult execute(String command) throws ServiceException {
    Session s = null;
    try {
        s = getSession();
        if (ZimbraLog.rmgmt.isDebugEnabled())
            ZimbraLog.rmgmt.debug("executing shim command  '" + mShimCommand + "' on " + this);
        s.execCommand(mShimCommand);
        OutputStream os = s.getStdin();
        String send = "HOST:" + mHost + " " + command;
        if (ZimbraLog.rmgmt.isDebugEnabled()) {
            ZimbraLog.rmgmt.debug("sending mgmt command '%s' on %s", send, this);
        }
        os.write(send.getBytes());
        os.close();
        RemoteResult result = new RemoteResult();
        InputStream stdout = new StreamGobbler(s.getStdout());
        InputStream stderr = new StreamGobbler(s.getStderr());
        result.mStdout = ByteUtil.getContent(stdout, -1);
        result.mStderr = ByteUtil.getContent(stderr, -1);
        if (ZimbraLog.rmgmt.isTraceEnabled()) {
            try {
                ZimbraLog.rmgmt.trace("stdout content for cmd:\n%s", new String(result.mStdout, "UTF-8"));
                ZimbraLog.rmgmt.trace("stderr content for cmd:\n%s", new String(result.mStderr, "UTF-8"));
            } catch (Exception ex) {
                ZimbraLog.rmgmt.trace("Problem logging stdout or stderr for cmd - probably not UTF-8");
            }
        }
        try {
            result.mExitStatus = s.getExitStatus();
        } catch (NullPointerException npe) {
        // wow this is strange - on hold command we hit NPE here.  TODO file a bug against ganymed
        }
        if (result.mExitStatus != 0) {
            throw new IOException("command failed: exit status=" + result.mExitStatus + ", stdout=" + new String(result.mStdout) + ", stderr=" + new String(result.mStderr));
        }
        result.mExitSignal = s.getExitSignal();
        return result;
    } catch (IOException ioe) {
        throw ServiceException.FAILURE("exception executing command: " + command + " with " + this, ioe);
    } finally {
        if (s != null) {
            releaseSession(s);
        }
    }
}
Also used : StreamGobbler(ch.ethz.ssh2.StreamGobbler) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) IOException(java.io.IOException) ServiceException(com.zimbra.common.service.ServiceException) 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