Module bus.extra

Class SshjSession

java.lang.Object
org.miaixz.bus.extra.ssh.provider.sshj.SshjSession
All Implemented Interfaces:
Closeable, Serializable, AutoCloseable, org.miaixz.bus.core.lang.Wrapper<Object>, org.miaixz.bus.core.Provider, Session

public class SshjSession extends Object implements Session
SSHJ-based Session implementation. This class provides a wrapper around the SSHJ library's session handling, offering functionalities for command execution, port forwarding, and SFTP integration. Project homepage: https://github.com/hierynomus/sshj
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • SshjSession

      public SshjSession(Connector connector)
      Constructs a SshjSession with the given Connector.
      Parameters:
      connector - The Connector holding connection and authentication information.
    • SshjSession

      public SshjSession(net.schmizz.sshj.SSHClient ssh)
      Constructs a SshjSession with a given SSHClient.
      Parameters:
      ssh - The SSHClient instance.
  • Method Details

    • getRaw

      public net.schmizz.sshj.connection.channel.direct.Session getRaw()
      Specified by:
      getRaw in interface org.miaixz.bus.core.lang.Wrapper<Object>
    • isConnected

      public boolean isConnected()
      Description copied from interface: Session
      Checks if the SSH session is currently connected and active.
      Specified by:
      isConnected in interface Session
      Returns:
      true if the session is connected, false otherwise.
    • close

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

      public SshjSftp openSftp(Charset charset)
      Opens an SFTP session.
      Parameters:
      charset - The character set for file names.
      Returns:
      A SshjSftp instance.
    • bindLocalPort

      public void bindLocalPort(InetSocketAddress localAddress, InetSocketAddress remoteAddress) throws org.miaixz.bus.core.lang.exception.InternalException
      Description copied from interface: Session
      Binds a local address (host and port) to a remote address, creating a local port forwarding tunnel. This is an extended version of Session.bindLocalPort(int, InetSocketAddress) that allows specifying the local bind address.
      Specified by:
      bindLocalPort in interface Session
      Parameters:
      localAddress - The local InetSocketAddress on the client machine to bind to.
      remoteAddress - The remote InetSocketAddress to forward traffic to.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException
    • unBindLocalPort

      public void unBindLocalPort(InetSocketAddress localAddress) throws org.miaixz.bus.core.lang.exception.InternalException
      Description copied from interface: Session
      Removes a local port forwarding binding for a specific local address.
      Specified by:
      unBindLocalPort in interface Session
      Parameters:
      localAddress - The local InetSocketAddress that was previously bound.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException
    • bindRemotePort

      public void bindRemotePort(InetSocketAddress remoteAddress, InetSocketAddress localAddress) throws org.miaixz.bus.core.lang.exception.InternalException
      Description copied from interface: Session
      Binds a port on the remote SSH server to a local address, creating a remote port forwarding tunnel. This is useful for allowing the remote server to access a service running on the local client machine. For example, a connection to remoteAddress on the SSH server will be forwarded through the tunnel to localAddress on the client machine.
      Specified by:
      bindRemotePort in interface Session
      Parameters:
      remoteAddress - The InetSocketAddress on the SSH server to bind.
      localAddress - The local InetSocketAddress to forward traffic to.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an error occurs while binding the remote port.
    • unBindRemotePort

      public void unBindRemotePort(InetSocketAddress remoteAddress)
      Description copied from interface: Session
      Removes a remote port forwarding binding.
      Specified by:
      unBindRemotePort in interface Session
      Parameters:
      remoteAddress - The remote InetSocketAddress that was previously bound.
    • exec

      public String exec(String cmd, Charset charset, OutputStream errStream)
      Executes a command using the 'exec' channel. This method sends a single command, does not read environment variables, and is non-blocking.
      Parameters:
      cmd - The command to execute.
      charset - The character set for sending and reading content.
      errStream - The output stream for error messages.
      Returns:
      The execution result.
    • execByShell

      public String execByShell(String cmd, Charset charset, OutputStream errStream)
      Executes a command within an interactive 'shell' channel. This method loads environment variables and may be blocking.
      Parameters:
      cmd - The command to execute.
      charset - The character set for sending and reading content.
      errStream - The output stream for error messages.
      Returns:
      The execution result.