Class SshClient

java.lang.Object
com.sshtools.client.SshClient
All Implemented Interfaces:
Closeable, AutoCloseable

public class SshClient extends Object implements Closeable
  • Field Details

    • GUEST_USERNAME

      public static final String GUEST_USERNAME
    • DEFAULT_CONNECT_TIMEOUT

      public static final long DEFAULT_CONNECT_TIMEOUT
  • Constructor Details

    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, long connectTimeout, char[] password) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      connectTimeout - timeout
      password - password
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, char[] password) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      password - password
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, char[] password, SshClientContext context) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      password - password
      sshContext - context
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, long connectTimeout, File key) throws IOException, com.sshtools.common.ssh.SshException, com.sshtools.common.publickey.InvalidPassphraseException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      connectTimeout - timeout
      key - key file
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      com.sshtools.common.publickey.InvalidPassphraseException
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, File key) throws IOException, com.sshtools.common.ssh.SshException, com.sshtools.common.publickey.InvalidPassphraseException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      key - key file
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      com.sshtools.common.publickey.InvalidPassphraseException
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, long connectTimeout, File key, String passphrase) throws IOException, com.sshtools.common.ssh.SshException, com.sshtools.common.publickey.InvalidPassphraseException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      connectTimeout - timeout
      key - key file
      passphrase - key passphrase
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      com.sshtools.common.publickey.InvalidPassphraseException
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, File key, String passphrase) throws IOException, com.sshtools.common.ssh.SshException, com.sshtools.common.publickey.InvalidPassphraseException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      key - key file
      passphrase - key passphrase
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      com.sshtools.common.publickey.InvalidPassphraseException
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, long connectTimeout, com.sshtools.common.ssh.components.SshKeyPair... identities) throws IOException, com.sshtools.common.ssh.SshException, com.sshtools.common.publickey.InvalidPassphraseException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      connectTimeout - timeout
      identities - identities
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      com.sshtools.common.publickey.InvalidPassphraseException
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, com.sshtools.common.ssh.components.SshKeyPair... identities) throws IOException, com.sshtools.common.ssh.SshException, com.sshtools.common.publickey.InvalidPassphraseException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      identities - identities
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      com.sshtools.common.publickey.InvalidPassphraseException
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, SshClientContext sshContext, long connectTimeout, com.sshtools.common.ssh.components.SshKeyPair... identities) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      sshContext - context
      connectTimeout - timeout
      identities - identities
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, SshClientContext sshContext, com.sshtools.common.ssh.components.SshKeyPair... identities) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      sshContext - context
      identities - identities
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, long connectTimeout, char[] password, com.sshtools.common.ssh.components.SshKeyPair... identities) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      connectTimeout - timeout
      password - password
      identities - identities
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, char[] password, com.sshtools.common.ssh.components.SshKeyPair... identities) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      password - password
      identities - identities
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, Integer port, String username, long connectTimeout, char[] password, File key, String passphrase) throws IOException, com.sshtools.common.ssh.SshException, com.sshtools.common.publickey.InvalidPassphraseException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      connectTimeout - timeout
      password - password
      key - key file
      passphrase - key passphrase
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      com.sshtools.common.publickey.InvalidPassphraseException
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, Integer port, String username, char[] password, File key, String passphrase) throws IOException, com.sshtools.common.ssh.SshException, com.sshtools.common.publickey.InvalidPassphraseException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      password - password
      key - key
      passphrase - passphrase
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      com.sshtools.common.publickey.InvalidPassphraseException
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, Integer port, String username, long connectTimeout) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      connectTimeout - timeout
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, Integer port, String username) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, Integer port, String username, SshClientContext sshContext, long connectTimeout) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      sshContext - context
      connectTimeout - timeout
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, Integer port, String username, SshClientContext sshContext) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      sshContext - context
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(com.sshtools.common.ssh.SshConnection con)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      sshContext - context
      connectTimeout - timeout
      password - password
      identities - identities
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(com.sshtools.common.ssh.SshConnection con, boolean closeConnection)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • SshClient

      @Deprecated(since="3.1.0", forRemoval=true) public SshClient(String hostname, int port, String username, SshClientContext sshContext, long connectTimeout, char[] password, com.sshtools.common.ssh.components.SshKeyPair... identities) throws IOException, com.sshtools.common.ssh.SshException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Create a new client.
      Parameters:
      hostname - host
      port - port
      username - user
      sshContext - context
      connectTimeout - timeout
      password - password
      identities - identities
      Throws:
      IOException - on I/O error
      com.sshtools.common.ssh.SshException - on general SSH error
      See Also:
  • Method Details

    • doConnect

      protected final com.sshtools.synergy.ssh.Connection<SshClientContext> doConnect(String hostname, int port, SshClientContext sshContext, long connectTimeout, List<String> keys) throws com.sshtools.common.ssh.SshException, IOException
      Throws:
      com.sshtools.common.ssh.SshException
      IOException
    • configure

      @Deprecated(since="3.1.0", forRemoval=true) protected void configure(SshClientContext sshContext) throws com.sshtools.common.ssh.SshException, IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Further configuration the client context.
      Parameters:
      sshContext -
      Throws:
      com.sshtools.common.ssh.SshException
      IOException
      See Also:
      • }.
    • addTask

      public Task addTask(Task task) throws IOException
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getContext

      public SshClientContext getContext()
    • getConnection

      public com.sshtools.synergy.ssh.Connection<SshClientContext> getConnection()
    • getForwardingPolicy

      public com.sshtools.common.forwarding.ForwardingPolicy getForwardingPolicy()
    • startLocalForwarding

      public int startLocalForwarding(String addressToBind, String destinationHost) throws com.sshtools.common.permissions.UnauthorizedException, com.sshtools.common.ssh.SshException
      Throws:
      com.sshtools.common.permissions.UnauthorizedException
      com.sshtools.common.ssh.SshException
    • startLocalForwarding

      public int startLocalForwarding(String addressToBind, int portToBind, String destinationHost, int destinationPort) throws com.sshtools.common.permissions.UnauthorizedException, com.sshtools.common.ssh.SshException
      Throws:
      com.sshtools.common.permissions.UnauthorizedException
      com.sshtools.common.ssh.SshException
    • stopLocalForwarding

      public void stopLocalForwarding(String addressToBind, int portToBind)
    • stopLocalForwarding

      public void stopLocalForwarding()
    • startRemoteForwarding

      public int startRemoteForwarding(String addressToBind, int portToBind, String destinationHost, int destinationPort) throws com.sshtools.common.ssh.SshException
      Throws:
      com.sshtools.common.ssh.SshException
    • startRemoteForwarding

      public int startRemoteForwarding(String addressToBind, String destinationHost) throws com.sshtools.common.ssh.SshException
      Throws:
      com.sshtools.common.ssh.SshException
    • stopRemoteForwarding

      public void stopRemoteForwarding(String addressToBind, int portToBind) throws com.sshtools.common.ssh.SshException
      Throws:
      com.sshtools.common.ssh.SshException
    • stopRemoteForwarding

      public void stopRemoteForwarding() throws com.sshtools.common.ssh.SshException
      Throws:
      com.sshtools.common.ssh.SshException
    • isConnected

      public boolean isConnected()
    • disconnect

      public void disconnect()
    • doTask

      protected <T extends Task> T doTask(T task, long timeout) throws IOException
      Throws:
      IOException
    • getFile

      public File getFile(String path) throws IOException
      Throws:
      IOException
    • getFile

      public File getFile(String path, long timeout) throws IOException
      Throws:
      IOException
    • getFile

      public void getFile(String path, File destination) throws IOException
      Throws:
      IOException
    • getFile

      public void getFile(String path, File destination, long timeout) throws IOException
      Throws:
      IOException
    • putFile

      public void putFile(File file) throws IOException
      Throws:
      IOException
    • putFile

      public void putFile(File file, String path) throws IOException
      Throws:
      IOException
    • putFile

      public void putFile(File file, String path, long timeout) throws IOException
      Throws:
      IOException
    • executeCommand

      public String executeCommand(String cmd) throws IOException
      Throws:
      IOException
    • executeCommand

      public String executeCommand(String cmd, long timeout) throws IOException
      Throws:
      IOException
    • executeCommand

      public String executeCommand(String cmd, String charset) throws IOException
      Throws:
      IOException
    • executeCommand

      public String executeCommand(String cmd, long timeout, String charset) throws IOException
      Throws:
      IOException
    • executeCommandWithResult

      public int executeCommandWithResult(String cmd, StringBuffer buffer) throws IOException
      Throws:
      IOException
    • executeCommandWithResult

      public int executeCommandWithResult(String cmd) throws IOException
      Throws:
      IOException
    • executeCommandWithResult

      public int executeCommandWithResult(String cmd, StringBuffer buffer, long timeout) throws IOException
      Throws:
      IOException
    • executeCommandWithResult

      public int executeCommandWithResult(String cmd, StringBuffer buffer, String charset) throws IOException
      Throws:
      IOException
    • executeCommandWithResult

      public int executeCommandWithResult(String cmd, StringBuffer buffer, long timeout, String charset) throws IOException
      Throws:
      IOException
    • getAuthenticationMethods

      public Set<String> getAuthenticationMethods()
    • authenticate

      public boolean authenticate(ClientAuthenticator authenticator, long timeout) throws IOException, com.sshtools.common.ssh.SshException
      Throws:
      IOException
      com.sshtools.common.ssh.SshException
    • isAuthenticated

      public boolean isAuthenticated()
    • runTask

      public <T extends Task> void runTask(T task, long timeout) throws IOException
      Throws:
      IOException
    • runTask

      public <T extends Task> void runTask(T task) throws IOException
      Throws:
      IOException
    • getRemotePublicKeys

      public String[] getRemotePublicKeys()
    • getRemoteIdentification

      public String getRemoteIdentification()
    • getLocalIdentification

      public String getLocalIdentification()
    • getHost

      public String getHost()
    • getHostKey

      public com.sshtools.common.ssh.components.SshPublicKey getHostKey()
    • openSessionChannel

      public SessionChannelNG openSessionChannel() throws com.sshtools.common.ssh.SshException
      Throws:
      com.sshtools.common.ssh.SshException
    • openSessionChannel

      public SessionChannelNG openSessionChannel(long timeout) throws com.sshtools.common.ssh.SshException
      Throws:
      com.sshtools.common.ssh.SshException
    • openSessionChannel

      public SessionChannelNG openSessionChannel(boolean autoConsume) throws com.sshtools.common.ssh.SshException
      Throws:
      com.sshtools.common.ssh.SshException
    • openSessionChannel

      public SessionChannelNG openSessionChannel(long timeout, boolean autoConsume) throws com.sshtools.common.ssh.SshException
      Throws:
      com.sshtools.common.ssh.SshException
    • openRemoteClient

      public SshClient openRemoteClient(String hostname, int port, String username) throws com.sshtools.common.ssh.SshException, IOException, com.sshtools.common.permissions.UnauthorizedException
      Throws:
      com.sshtools.common.ssh.SshException
      IOException
      com.sshtools.common.permissions.UnauthorizedException
    • getPort

      public int getPort()
    • main

      public static void main(String[] args) throws IOException, com.sshtools.common.ssh.SshException
      Throws:
      IOException
      com.sshtools.common.ssh.SshException