Example usage for org.apache.commons.net.ssh SSHClient startSession

List of usage examples for org.apache.commons.net.ssh SSHClient startSession

Introduction

In this page you can find the example usage for org.apache.commons.net.ssh SSHClient startSession.

Prototype

public Session startSession() throws ConnectionException, TransportException 

Source Link

Document

Opens a session channel.

Usage

From source file:examples.ssh.Exec.java

public static void main(String... args) throws Exception {
    SSHClient ssh = new SSHClient();
    ssh.loadKnownHosts();/*from w w w. ja v a2 s  .c  om*/

    ssh.connect("localhost");
    try {

        ssh.authPublickey(System.getProperty("user.name"));

        Command cmd = ssh.startSession().exec("man sshd_config");

        // Pipe.pipe(cmd.getInputStream(), System.out, cmd.getLocalMaxPacketSize(), false);
        System.out.print(cmd.getOutputAsString());
        System.out.println("\n** exit status: " + cmd.getExitStatus());

    } finally {
        ssh.disconnect();
    }
}

From source file:examples.ssh.RudimentaryPTY.java

public static void main(String... args) throws IOException {
    SSHClient ssh = new SSHClient();

    ssh.loadKnownHosts();//from w w  w  .  j a v a2  s.  c om

    ssh.connect("localhost");

    Shell shell = null;

    try {

        ssh.authPublickey(System.getProperty("user.name"));

        Session session = ssh.startSession();
        session.allocateDefaultPTY();

        shell = session.startShell();

        new Pipe("stdout", shell.getInputStream(), System.out) //
                .bufSize(shell.getLocalMaxPacketSize()) //
                .start();

        new Pipe("stderr", shell.getErrorStream(), System.err) //
                .bufSize(shell.getLocalMaxPacketSize()) //
                .start();

        // Now make System.in act as stdin. To exit, hit Ctrl+D (since that results in an EOF on System.in)

        // This is kinda messy because java only allows console input after you hit return
        // But this is just an example... a GUI app could implement a proper PTY

        Pipe.pipe(System.in, shell.getOutputStream(), shell.getRemoteMaxPacketSize(), false);

    } finally {

        if (shell != null)
            shell.close();

        ssh.disconnect();
    }
}

From source file:examples.ssh.X11.java

public static void main(String... args) throws Exception {
    SSHClient ssh = new SSHClient();

    // Compression makes X11 more feasible over slower connections
    // ssh.useCompression();

    ssh.loadKnownHosts();/*from   www .  ja  va2s .c  o m*/

    /*
     * NOTE: Forwarding incoming X connections to localhost:6000 only works if X is started without the
     * "-nolisten tcp" option (this is usually not the default for good reason)
     */
    ssh.registerX11Forwarder(new SocketForwardingConnectListener(new InetSocketAddress("localhost", 6000)));

    ssh.connect("localhost");
    try {

        ssh.authPublickey(System.getProperty("user.name"));

        Session sess = ssh.startSession();

        /*
         * It is recommendable to send a fake cookie, and in your ConnectListener when a connection comes in replace
         * it with the real one. But here simply one from `xauth list` is being used.
         */
        sess.reqX11Forwarding("MIT-MAGIC-COOKIE-1", "26e8700422fd3efb99a918ce02324e9e", 0);

        Command cmd = sess.exec("firefox");

        new Pipe("stdout", cmd.getInputStream(), System.out).start();
        new Pipe("stderr", cmd.getErrorStream(), System.err).start();

        // Wait for session & X11 channel to get closed
        ssh.getConnection().join();

    } finally {
        ssh.disconnect();
    }
}