Example usage for org.apache.commons.net.ssh.connection SocketForwardingConnectListener SocketForwardingConnectListener

List of usage examples for org.apache.commons.net.ssh.connection SocketForwardingConnectListener SocketForwardingConnectListener

Introduction

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

Prototype

public SocketForwardingConnectListener(SocketAddress addr) 

Source Link

Document

Create with a SocketAddress this listener will forward to.

Usage

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 ww w . j  a va  2s .  co 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();
    }
}

From source file:examples.ssh.RemotePF.java

public static void main(String... args) throws Exception {
    SSHClient client = new SSHClient();
    client.loadKnownHosts();/*from w w w  . j a v a2s .  c o  m*/

    client.connect("localhost");
    try {

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

        /*
         * We make _server_ listen on port 8080, which forwards all connections to us as a channel, and we further
         * forward all such channels to google.com:80
         */
        client.getRemotePortForwarder().bind(new Forward(8080), //
                new SocketForwardingConnectListener(new InetSocketAddress("google.com", 80)));

        // Something to hang on to so forwarding stays
        client.getTransport().join();

    } finally {
        client.disconnect();
    }
}