Class SshjSession

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

public class SshjSession extends Object implements Session
基于SSHJ(https://github.com/hierynomus/sshj)的Session封装
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • SshjSession

      public SshjSession(Connector connector)
      构造
      Parameters:
      connector - Connector,保存连接和验证信息等
    • SshjSession

      public SshjSession(net.schmizz.sshj.SSHClient ssh)
      构造
      Parameters:
      ssh - SSHClient
  • 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
      是否连接状态
      Specified by:
      isConnected in interface Session
      Returns:
      是否连接状态
    • 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)
      打开SFTP会话
      Parameters:
      charset - 编码
      Returns:
      SshjSftp
    • bindLocalPort

      public void bindLocalPort(InetSocketAddress localAddress, InetSocketAddress remoteAddress) throws org.miaixz.bus.core.lang.exception.InternalException
      Description copied from interface: Session
      绑定端口到本地。 一个会话可绑定多个端口 当请求localHost:localPort时,通过SSH到服务器,转发请求到remoteHost:remotePort 此方法用于访问本地无法访问但是服务器可以访问的地址,如只有服务器能访问的内网数据库等
      Specified by:
      bindLocalPort in interface Session
      Parameters:
      localAddress - 本地主机和端口
      remoteAddress - 远程主机和端口
      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
      解除本地端口映射
      Specified by:
      unBindLocalPort in interface Session
      Parameters:
      localAddress - 需要解除的本地地址
      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
      绑定ssh服务端的serverPort端口, 到本地主机的port端口上. 即数据从ssh服务端的serverPort端口, 流经ssh客户端, 达到host:port上. 此方法用于在服务端访问本地资源,如服务器访问本机所在的数据库等。
      Specified by:
      bindRemotePort in interface Session
      Parameters:
      remoteAddress - ssh服务端上要被绑定的地址
      localAddress - 转发到的本地地址
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - 端口绑定失败异常
    • unBindRemotePort

      public void unBindRemotePort(InetSocketAddress remoteAddress)
      Description copied from interface: Session
      解除远程端口映射
      Specified by:
      unBindRemotePort in interface Session
      Parameters:
      remoteAddress - 需要解除的远程地址和端口
    • exec

      public String exec(String cmd, Charset charset, OutputStream errStream)
      执行Shell命令(使用EXEC方式)

      此方法单次发送一个命令到服务端,不读取环境变量,不会产生阻塞。

      Parameters:
      cmd - 命令
      charset - 发送和读取内容的编码
      errStream - 错误信息输出到的位置
      Returns:
      执行返回结果
    • execByShell

      public String execByShell(String cmd, Charset charset, OutputStream errStream)
      执行Shell命令

      此方法单次发送一个命令到服务端,自动读取环境变量,可能产生阻塞。

      Parameters:
      cmd - 命令
      charset - 发送和读取内容的编码
      errStream - 错误信息输出到的位置
      Returns:
      执行返回结果