Search in sources :

Example 6 with StreamGobbler

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

the class Basic 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.
			 * 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) Session(com.trilead.ssh2.Session)

Example 7 with StreamGobbler

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

the class PublicKeyAuthentication method main.

public static void main(String[] args) {
    String hostname = "127.0.0.1";
    String username = "joe";
    // or "~/.ssh/id_dsa"
    File keyfile = new File("~/.ssh/id_rsa");
    // will be ignored if not needed
    String keyfilePass = "joespass";
    try {
        /* Create a connection instance */
        Connection conn = new Connection(hostname);
        /* Now connect */
        conn.connect();
        /* Authenticate */
        boolean isAuthenticated = conn.authenticateWithPublicKey(username, keyfile, keyfilePass);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("uname -a && date && uptime && who");
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        System.out.println("Here is some information about the remote host:");
        while (true) {
            String line = br.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) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 8 with StreamGobbler

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

the class UsingKnownHosts method main.

public static void main(String[] args) throws IOException {
    String hostname = "somehost";
    String username = "joe";
    String password = "joespass";
    File knownHosts = new File("~/.ssh/known_hosts");
    try {
        if (knownHosts.exists())
            database.addHostkeys(knownHosts);
        /* Create a connection instance */
        Connection conn = new Connection(hostname);
        /* Now connect and use the SimpleVerifier */
        conn.connect(new SimpleVerifier(database));
        /* Authenticate */
        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");
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        System.out.println("Here is some information about the remote host:");
        while (true) {
            String line = br.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) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 9 with StreamGobbler

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

the class SshSessionConnection method open.

public void open(IStreamLogger streamLogger) throws AuthenticationException {
    SshLogger.debug("opening session");
    mySession = mySessionProvider.compute();
    // wrapper created, inspection is inapplicable
    //noinspection IOResourceOpenedButNotSafelyClosed
    myInputStream = new MyInputStreamWrapper(myActivityMonitor, mySession.getStdout());
    // wrapper created, inspection is inapplicable
    //noinspection IOResourceOpenedButNotSafelyClosed
    myOutputStream = new MyOutputStreamWrapper(myActivityMonitor, mySession.getStdin());
    myErrorStreamGobbler = new StreamGobbler(mySession.getStderr());
    myState = LifeStages.CREATED;
}
Also used : StreamGobbler(com.trilead.ssh2.StreamGobbler)

Example 10 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