Search in sources :

Example 1 with LocalPortForwarder

use of ch.ethz.ssh2.LocalPortForwarder in project intellij-community by JetBrains.

the class PortForwarding 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.");
        /* ===== OK, now let's establish some local port forwardings ===== */
        /* Example Port Forwarding: -L 8080:www.icann.org:80 (OpenSSH notation)
			 * 
			 * This works by allocating a socket to listen on 8080 on the local interface (127.0.0.1).
			 * Whenever a connection is made to this port (127.0.0.1:8080), the connection is forwarded
			 * over the secure channel, and a connection is made to www.icann.org:80 from the remote
			 * machine (i.e., the ssh server).
			 * 
			 * (the above text is based partially on the OpenSSH man page)
			 */
        /* You can create as many of them as you want */
        LocalPortForwarder lpf1 = conn.createLocalPortForwarder(8080, "www.icann.org", 80);
        /* Now simply point your webbrowser to 127.0.0.1:8080 */
        /* (on the host where you execute this program)                         */
        /* ===== OK, now let's establish some remote port forwardings ===== */
        /* Example Port Forwarding: -R 127.0.0.1:8080:www.ripe.net:80 (OpenSSH notation)
			 * 
			 * Specifies that the port 127.0.0.1:8080 on the remote server is to be forwarded to the
			 * given host and port on the local side.  This works by allocating a socket to listen to port
			 * 8080 on the remote side (the ssh server), and whenever a connection is made to this port, the
			 * connection is forwarded over the secure channel, and a connection is made to
			 * www.ripe.net:80 by the Trilead SSH-2 library.
			 * 
			 * (the above text is based partially on the OpenSSH man page)
			 */
        /* You can create as many of them as you want */
        conn.requestRemotePortForwarding("127.0.0.1", 8080, "www.ripe.net", 80);
        /* Now, on the ssh server, if you connect to 127.0.0.1:8080, then the connection is forwarded
			 * through the secure tunnel to the library, which in turn will forward the connection
			 * to www.ripe.net:80. */
        /* Sleep a bit... (30 seconds) */
        sleepSomeTime(30000);
        /* Stop accepting remote connections that are being forwarded to www.ripe.net:80 */
        conn.cancelRemotePortForwarding(8080);
        /* Sleep a bit... (20 seconds) */
        sleepSomeTime(20000);
        /* Stop accepting connections on 127.0.0.1:8080 that are being forwarded to www.icann.org:80 */
        lpf1.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : LocalPortForwarder(com.trilead.ssh2.LocalPortForwarder) Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException) File(java.io.File)

Example 2 with LocalPortForwarder

use of ch.ethz.ssh2.LocalPortForwarder in project agileway by fangjinuo.

the class Ssh2ForwardingClient method startLocalForwarding.

@Override
public ForwardingChannelInfo startLocalForwarding(String bindToHost, int bindToPort, String destHost, int destPort) throws SshException {
    ForwardingChannelInfo channel = new ForwardingChannelInfo(ForwardingChannelInfo.LOCAL_FORWARDING_CHANNEL, bindToHost, bindToPort, destHost, destPort);
    if (!localForwarderMap.containsKey(ForwardingChannelInfo.id(channel))) {
        Connection delegate = this.connection.getDelegate();
        try {
            LocalPortForwarder localPortForwarder = delegate.createLocalPortForwarder(new InetSocketAddress(bindToHost, bindToPort), destHost, destPort);
            localForwarderMap.put(ForwardingChannelInfo.id(channel), localPortForwarder);
        } catch (Throwable ex) {
            throw new SshException(ex);
        }
    }
    return channel;
}
Also used : LocalPortForwarder(com.trilead.ssh2.LocalPortForwarder) InetSocketAddress(java.net.InetSocketAddress) Connection(com.trilead.ssh2.Connection) ForwardingChannelInfo(com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo) SshException(com.jn.agileway.ssh.client.SshException)

Example 3 with LocalPortForwarder

use of ch.ethz.ssh2.LocalPortForwarder in project agileway by fangjinuo.

the class Ssh2ForwardingClient method startLocalForwarding.

@Override
public ForwardingChannelInfo startLocalForwarding(String bindToHost, int bindToPort, String destHost, int destPort) throws SshException {
    ForwardingChannelInfo channel = new ForwardingChannelInfo(ForwardingChannelInfo.LOCAL_FORWARDING_CHANNEL, bindToHost, bindToPort, destHost, destPort);
    LocalPortForwarder forwarder = localForwarderMap.get(channel);
    if (forwarder == null) {
        try {
            ch.ethz.ssh2.Connection delegate = this.connection.getDelegate();
            forwarder = delegate.createLocalPortForwarder(bindToPort, destHost, destPort);
            localForwarderMap.put(ForwardingChannelInfo.id(channel), forwarder);
        } catch (Throwable ex) {
            throw new SshException(ex);
        }
    }
    return channel;
}
Also used : LocalPortForwarder(ch.ethz.ssh2.LocalPortForwarder) ForwardingChannelInfo(com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo) SshException(com.jn.agileway.ssh.client.SshException)

Aggregations

SshException (com.jn.agileway.ssh.client.SshException)2 ForwardingChannelInfo (com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo)2 Connection (com.trilead.ssh2.Connection)2 LocalPortForwarder (com.trilead.ssh2.LocalPortForwarder)2 LocalPortForwarder (ch.ethz.ssh2.LocalPortForwarder)1 File (java.io.File)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1