Class SSHUtil


  • public class SSHUtil
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_SSH_PORT  
      private static org.slf4j.Logger LOGGER  
      private static java.lang.String VIA_TAG  
    • Constructor Summary

      Constructors 
      Constructor Description
      SSHUtil()
      This constructor is only needed for testing.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static SSHConnection connect​(java.lang.String adaptorName, org.apache.sshd.client.SshClient client, java.lang.String location, Credential credential, int bufferSize, long timeout)
      Connect an existing SshClient to the server at location and authenticate using the given credential.
      private static org.apache.sshd.client.session.ClientSession connectAndAuthenticate​(java.lang.String adaptorName, org.apache.sshd.client.SshClient client, java.lang.String host, int port, UserCredential credential, long timeout)  
      static org.apache.sshd.client.SshClient createSSHClient()
      Create a new SshClient with a default configuration similar to a stand-alone SSH client.
      static org.apache.sshd.client.SshClient createSSHClient​(boolean useKnownHosts, boolean loadSSHConfig, boolean stricHostCheck, boolean useSSHAgent, boolean useAgentForwarding)
      Create a new SshClient with the desired configuration.
      static UserCredential extractCredential​(org.apache.sshd.common.util.net.SshdSocketAddress location, Credential credential)  
      static UserCredential[] extractCredentials​(java.lang.String adaptorName, org.apache.sshd.common.util.net.SshdSocketAddress[] locations, Credential credentials)  
      static org.apache.sshd.common.util.net.SshdSocketAddress[] extractLocations​(java.lang.String adaptorName, java.lang.String location)
      Extract a series of locations from a location string.
      private static org.apache.sshd.common.util.net.SshdSocketAddress extractSocketAddress​(java.lang.String adaptorName, java.lang.String location)  
      static java.lang.String getHost​(java.lang.String adaptorName, java.lang.String location)  
      static int getPort​(java.lang.String adaptorName, java.lang.String location)  
      static java.util.Map<java.lang.String,​java.lang.String> translateProperties​(java.util.Map<java.lang.String,​java.lang.String> providedProperties, java.lang.String orginalPrefix, XenonPropertyDescription[] supportedProperties, java.lang.String newPrefix)  
      static java.lang.String validateHost​(java.lang.String adaptorName, java.lang.String host)
      Weak validation of a host string containing either a hostame of IP adres.
      static java.util.Set<java.lang.String> validProperties​(XenonPropertyDescription[] properties)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
    • Constructor Detail

      • SSHUtil

        public SSHUtil()
        This constructor is only needed for testing. Users should use the static methods instead.
    • Method Detail

      • createSSHClient

        public static org.apache.sshd.client.SshClient createSSHClient()
        Create a new SshClient with a default configuration similar to a stand-alone SSH client.

        The default configuration loads the SSH known_hosts and config file and uses strict host key checking.

        Returns:
        the configured SshClient
      • createSSHClient

        public static org.apache.sshd.client.SshClient createSSHClient​(boolean useKnownHosts,
                                                                       boolean loadSSHConfig,
                                                                       boolean stricHostCheck,
                                                                       boolean useSSHAgent,
                                                                       boolean useAgentForwarding)
        Create a new SshClient with the desired configuration.

        SSH clients have a significant number of options. This method will create a SshClient providing the most important settings.

        Parameters:
        useKnownHosts - Load the SSH known_hosts file from the default location (for OpenSSH this is typically found in $HOME/.ssh/known_hosts).
        loadSSHConfig - Load the SSH config file from the default location (for OpenSSH this is typically found in $HOME/.ssh/config).
        stricHostCheck - Perform a strict host key check. When setting up a connection, the key presented by the server is compared to the default known_hosts file (for OpenSSH this is typically found in $HOME/.ssh/known_hosts).
        useSSHAgent - When setting up a connection, handoff authentication to a separate SSH agent process.
        useAgentForwarding - Support agent forwarding, allowing remote SSH servers to use the local SSH agent process to authenticate connections to other servers.
        Returns:
        the configured SshClient
      • validateHost

        public static java.lang.String validateHost​(java.lang.String adaptorName,
                                                    java.lang.String host)
                                             throws InvalidLocationException
        Weak validation of a host string containing either a hostame of IP adres.
        Parameters:
        adaptorName - the name of the adaptor using this method.
        host - the hostname to validate
        Returns:
        the value of host if the validation succeeded.
        Throws:
        InvalidLocationException - if the validation failed
      • extractCredential

        public static UserCredential extractCredential​(org.apache.sshd.common.util.net.SshdSocketAddress location,
                                                       Credential credential)
      • extractLocations

        public static org.apache.sshd.common.util.net.SshdSocketAddress[] extractLocations​(java.lang.String adaptorName,
                                                                                           java.lang.String location)
                                                                                    throws InvalidLocationException
        Extract a series of locations from a location string. This method will split the location string into the destination and any number of intermediate hops. The accepted format is:

        "host[:port][/workdir] [via:otherhost[:port]]*"

        The locations will be returned in connection setup order, which is the reverse order in which they are listed in the location string.
        Parameters:
        adaptorName - the adaptor calling this method (used in exceptions).
        location - the location to parse
        Returns:
        the location string split into its individual locations.
        Throws:
        InvalidLocationException - if the provided location is could not be parsed.
      • connectAndAuthenticate

        private static org.apache.sshd.client.session.ClientSession connectAndAuthenticate​(java.lang.String adaptorName,
                                                                                           org.apache.sshd.client.SshClient client,
                                                                                           java.lang.String host,
                                                                                           int port,
                                                                                           UserCredential credential,
                                                                                           long timeout)
                                                                                    throws XenonException
        Throws:
        XenonException
      • connect

        public static SSHConnection connect​(java.lang.String adaptorName,
                                            org.apache.sshd.client.SshClient client,
                                            java.lang.String location,
                                            Credential credential,
                                            int bufferSize,
                                            long timeout)
                                     throws XenonException
        Connect an existing SshClient to the server at location and authenticate using the given credential.
        Parameters:
        adaptorName - the adaptor where this method was called from.
        client - the client to connect.
        location - the server to connect to
        credential - the credential to authenticate with.
        bufferSize - the buffer size used for the (optional) SSH tunnels.
        timeout - the timeout to use in connection setup (in milliseconds).
        Returns:
        the connected ClientSession
        Throws:
        XenonException - if the connection setup or authentication failed.
      • translateProperties

        public static java.util.Map<java.lang.String,​java.lang.String> translateProperties​(java.util.Map<java.lang.String,​java.lang.String> providedProperties,
                                                                                                 java.lang.String orginalPrefix,
                                                                                                 XenonPropertyDescription[] supportedProperties,
                                                                                                 java.lang.String newPrefix)
      • validProperties

        public static java.util.Set<java.lang.String> validProperties​(XenonPropertyDescription[] properties)