Module bus.extra

Interface Session

All Superinterfaces:
AutoCloseable, Closeable, org.miaixz.bus.core.Provider, Serializable, org.miaixz.bus.core.lang.Wrapper<Object>
All Known Implementing Classes:
JschSession, SshjSession

public interface Session extends org.miaixz.bus.core.lang.Wrapper<Object>, Closeable
Abstract interface for an SSH (Secure Shell) session. This interface defines common operations for managing an SSH connection, including checking the connection status and handling port forwarding (tunnelling). It extends Wrapper to provide access to the underlying session object and Closeable for resource management.
Since:
Java 17+
Author:
Kimi Liu
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    bindLocalPort(int localPort, InetSocketAddress remoteAddress)
    Binds a local port to a remote address, creating a local port forwarding tunnel.
    void
    bindLocalPort(InetSocketAddress localAddress, InetSocketAddress remoteAddress)
    Binds a local address (host and port) to a remote address, creating a local port forwarding tunnel.
    void
    bindRemotePort(InetSocketAddress remoteAddress, InetSocketAddress localAddress)
    Binds a port on the remote SSH server to a local address, creating a remote port forwarding tunnel.
    boolean
    Checks if the SSH session is currently connected and active.
    default void
    unBindLocalPort(int localPort)
    Removes a local port forwarding binding.
    void
    Removes a local port forwarding binding for a specific local address.
    void
    Removes a remote port forwarding binding.

    Methods inherited from interface java.io.Closeable

    close

    Methods inherited from interface org.miaixz.bus.core.lang.Wrapper

    getRaw, type
  • Method Details

    • isConnected

      boolean isConnected()
      Checks if the SSH session is currently connected and active.
      Returns:
      true if the session is connected, false otherwise.
    • bindLocalPort

      default void bindLocalPort(int localPort, InetSocketAddress remoteAddress)
      Binds a local port to a remote address, creating a local port forwarding tunnel. This is useful for accessing a service on a remote network that is only accessible from the SSH server. For example, a request to localhost:localPort on the client machine will be forwarded through the SSH tunnel to remoteHost:remotePort on the server's network.
      Parameters:
      localPort - The local port on the client machine to listen on.
      remoteAddress - The InetSocketAddress of the remote host and port to forward traffic to.
    • bindLocalPort

      void bindLocalPort(InetSocketAddress localAddress, InetSocketAddress remoteAddress)
      Binds a local address (host and port) to a remote address, creating a local port forwarding tunnel. This is an extended version of bindLocalPort(int, InetSocketAddress) that allows specifying the local bind address.
      Parameters:
      localAddress - The local InetSocketAddress on the client machine to bind to.
      remoteAddress - The remote InetSocketAddress to forward traffic to.
    • unBindLocalPort

      default void unBindLocalPort(int localPort)
      Removes a local port forwarding binding.
      Parameters:
      localPort - The local port that was previously bound.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an error occurs while unbinding the port.
    • unBindLocalPort

      void unBindLocalPort(InetSocketAddress localAddress)
      Removes a local port forwarding binding for a specific local address.
      Parameters:
      localAddress - The local InetSocketAddress that was previously bound.
    • bindRemotePort

      void bindRemotePort(InetSocketAddress remoteAddress, InetSocketAddress localAddress)
      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.
      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

      void unBindRemotePort(InetSocketAddress remoteAddress)
      Removes a remote port forwarding binding.
      Parameters:
      remoteAddress - The remote InetSocketAddress that was previously bound.