Module bus.extra

Class JschSession

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

public class JschSession extends Object implements Session
JSch Session encapsulation. This class implements the Session interface and provides a wrapper around the JSch Session, offering functionalities for command execution, port forwarding, and channel management.
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • JschSession

      public JschSession(Connector connector)
      Constructs a JschSession with the given Connector. This creates a new JSch session based on the provided connection and authentication information.
      Parameters:
      connector - The Connector holding connection and authentication details.
    • JschSession

      public JschSession(com.jcraft.jsch.Session raw, long timeout)
      Constructs a JschSession by wrapping a raw JSch Session and a timeout.
      Parameters:
      raw - The raw JSch Session to wrap.
      timeout - The connection timeout duration in milliseconds; 0 indicates no limit.
  • Method Details

    • getRaw

      public com.jcraft.jsch.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
    • 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)
      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.
    • 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.
    • createChannel

      public com.jcraft.jsch.Channel createChannel(ChannelType channelType)
      Creates an SSH channel but does not connect it.
      Parameters:
      channelType - The type of channel to create (e.g., shell, sftp), see ChannelType.
      Returns:
      An unconnected JSch Channel object.
    • openShell

      public com.jcraft.jsch.ChannelShell openShell()
      Opens and connects an interactive Shell channel.
      Returns:
      A connected ChannelShell object.
    • openChannel

      public com.jcraft.jsch.Channel openChannel(ChannelType channelType)
      Opens and connects an SSH channel of a specified type.
      Parameters:
      channelType - The type of channel to open (e.g., shell, sftp), see ChannelType.
      Returns:
      A connected JSch Channel object.
    • openSftp

      public JschSftp openSftp(Charset charset)
      Opens an SFTP session, returning a wrapper for SFTP operations.
      Parameters:
      charset - The character set to use for file names.
      Returns:
      A JschSftp instance for performing SFTP operations.
    • exec

      public String exec(String cmd, Charset charset)
      Executes a command using the 'exec' channel and returns the output.
      Parameters:
      cmd - The command to execute.
      charset - The character set for encoding the command and decoding the output.
      Returns:
      The execution result as a string.
    • exec

      public String exec(String cmd, Charset charset, OutputStream errStream)
      Executes a command using the 'exec' channel and returns the output. This method is non-interactive, sends a single command, and does not load the user's shell profile. The channel is automatically closed after execution.
      Parameters:
      cmd - The command to execute.
      charset - The character set for encoding the command and decoding the output.
      errStream - The OutputStream to which error messages will be written.
      Returns:
      The execution result as a string.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error or JSch exception occurs, or if the command returns a non-zero exit status.
    • execByShell

      public String execByShell(String cmd, Charset charset)
      Executes a command within an interactive 'shell' channel. This method simulates typing a command into a shell, which means the user's profile and environment variables are loaded. The channel is automatically closed after execution.
      Parameters:
      cmd - The command to execute.
      charset - The character set for sending and reading content.
      Returns:
      The execution result content.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.