com.numdata.ssh
Class SshTools

java.lang.Object
  extended by com.numdata.ssh.SshTools

public class SshTools
extends Object

Some tools for using SSH connections.

Author:
Peter S. Heijnen

Nested Class Summary
static class SshTools.PortForwardingProperties
          Defines local or remote port forwarding properties.
 
Method Summary
static ch.ethz.ssh2.Connection connect(String server)
          Connect to SSH server.
static ch.ethz.ssh2.Connection connect(String host, int port, String username, String password)
          Connect to SSH server.
static JdbcDataSource createJdbcDataSource(String sshServer, String dbServer, int dbPort, String jdbcDriver, String jdbcUrl, String jdbcUser, String jdbcPassword)
          Create JdbcDataSource.
static int createLocalPortForwarder(ch.ethz.ssh2.Connection connection, SshTools.PortForwardingProperties properties)
          Create local port forwarding based on the specified SshTools.PortForwardingProperties.
static int createLocalPortForwarder(ch.ethz.ssh2.Connection connection, String targetHost, int targetPort)
          Create port forwarding tunnel to the specified target host and port and return the local TCP port through which it can be accessed.
static int getFreeLocalPort()
          Get any free local TCP port (typically used for local port forwarding).
static Quartet<String,Integer,String,String> parseOptions(String server)
          Parse SSH server specification.
static int requestRemotePortForwarding(ch.ethz.ssh2.Connection connection, SshTools.PortForwardingProperties properties)
          Request remote port forwarding based on the specified SshTools.PortForwardingProperties.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

connect

public static ch.ethz.ssh2.Connection connect(@Nullable
                                              String server)
                                       throws IOException
Connect to SSH server.

Parameters:
server - SSH server specification (<user>:<pass>@<host>[':'<port>]).
Returns:
Open connection.
Throws:
IOException - if the connection could not be established.
IllegalArgumentException - if the specification is badly formatted.

parseOptions

@Nullable
public static Quartet<String,Integer,String,String> parseOptions(@Nullable
                                                                          String server)
Parse SSH server specification. Syntax:
<user>:<pass>@<host>[':'<port>]
.

Parameters:
server - SSH server specification.
Returns:
Quartet with SSH server host, server port, user name, and password; null if server is empty.
Throws:
IllegalArgumentException - if the specification is badly formatted.

connect

@NotNull
public static ch.ethz.ssh2.Connection connect(@NotNull
                                                      String host,
                                                      int port,
                                                      @NotNull
                                                      String username,
                                                      @NotNull
                                                      String password)
                                       throws IOException
Connect to SSH server.

Parameters:
host - SSH server host.
port - SSH server port.
username - User name for authentication.
password - Password for authentication.
Returns:
Open connection
Throws:
IOException - if the connection could not be established.

createJdbcDataSource

public static JdbcDataSource createJdbcDataSource(@Nullable
                                                  String sshServer,
                                                  @NotNull
                                                  String dbServer,
                                                  int dbPort,
                                                  String jdbcDriver,
                                                  @NotNull
                                                  String jdbcUrl,
                                                  String jdbcUser,
                                                  String jdbcPassword)
                                           throws IOException,
                                                  SQLException
Create JdbcDataSource.

Parameters:
sshServer - Optional SSH server specification (<user>:<pass>@<host>[':'<port>]).
dbServer - Database server host.
dbPort - TCP port of database server.
jdbcDriver - JDBC driver name.
jdbcUrl - JDBC URL format ({0}=host, {1}=port).
jdbcUser - JDBC user name.
jdbcPassword - JDBC password.
Returns:
JdbcDataSource.
Throws:
IllegalArgumentException - if the specification is badly formatted.
IOException - if the SSH connection could not be established.
SQLException - if an error occurs while accessing the database.

createLocalPortForwarder

public static int createLocalPortForwarder(@NotNull
                                           ch.ethz.ssh2.Connection connection,
                                           String targetHost,
                                           int targetPort)
                                    throws IOException
Create port forwarding tunnel to the specified target host and port and return the local TCP port through which it can be accessed.

Parameters:
connection - Connection to create port forwarding on.
targetHost - Target host to connect to.
targetPort - TCP port to connect to.
Returns:
Local TCP port.
Throws:
IOException - if the tunnel could not be created.

createLocalPortForwarder

public static int createLocalPortForwarder(@NotNull
                                           ch.ethz.ssh2.Connection connection,
                                           @NotNull
                                           SshTools.PortForwardingProperties properties)
                                    throws IOException
Create local port forwarding based on the specified SshTools.PortForwardingProperties.

Parameters:
connection - Connection to create port forwardings on.
properties - Port forwarding properties.
Returns:
Local TCP port.
Throws:
IOException - port forwarding failed.

requestRemotePortForwarding

public static int requestRemotePortForwarding(@NotNull
                                              ch.ethz.ssh2.Connection connection,
                                              @NotNull
                                              SshTools.PortForwardingProperties properties)
                                       throws IOException
Request remote port forwarding based on the specified SshTools.PortForwardingProperties.

Parameters:
connection - Connection to request port forwardings on.
properties - Port forwarding properties.
Returns:
Local TCP port.
Throws:
IOException - port forwarding failed.

getFreeLocalPort

public static int getFreeLocalPort()
                            throws IOException
Get any free local TCP port (typically used for local port forwarding).

Returns:
Free local TCP port.
Throws:
IOException - if no free local TCP port is available.


Copyright © 2017 Numdata BV. All rights reserved.