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>,org.miaixz.bus.core.Provider,Session
基于SSHJ(https://github.com/hierynomus/sshj)的Session封装
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
ConstructorsConstructorDescriptionSshjSession(net.schmizz.sshj.SSHClient ssh) 构造SshjSession(Connector connector) 构造 -
Method Summary
Modifier and TypeMethodDescriptionvoidbindLocalPort(InetSocketAddress localAddress, InetSocketAddress remoteAddress) 绑定端口到本地。voidbindRemotePort(InetSocketAddress remoteAddress, InetSocketAddress localAddress) 绑定ssh服务端的serverPort端口, 到本地主机的port端口上.voidclose()exec(String cmd, Charset charset, OutputStream errStream) 执行Shell命令(使用EXEC方式)execByShell(String cmd, Charset charset, OutputStream errStream) 执行Shell命令net.schmizz.sshj.connection.channel.direct.SessiongetRaw()boolean是否连接状态打开SFTP会话voidunBindLocalPort(InetSocketAddress localAddress) 解除本地端口映射voidunBindRemotePort(InetSocketAddress remoteAddress) 解除远程端口映射Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.miaixz.bus.extra.ssh.Session
bindLocalPort, unBindLocalPortMethods inherited from interface org.miaixz.bus.core.lang.Wrapper
type
-
Constructor Details
-
SshjSession
构造- 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:
getRawin interfaceorg.miaixz.bus.core.lang.Wrapper<Object>
-
isConnected
public boolean isConnected()Description copied from interface:Session是否连接状态- Specified by:
isConnectedin interfaceSession- Returns:
- 是否连接状态
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
openSftp
打开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:
bindLocalPortin interfaceSession- 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:
unBindLocalPortin interfaceSession- 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:
bindRemotePortin interfaceSession- Parameters:
remoteAddress- ssh服务端上要被绑定的地址localAddress- 转发到的本地地址- Throws:
org.miaixz.bus.core.lang.exception.InternalException- 端口绑定失败异常
-
unBindRemotePort
Description copied from interface:Session解除远程端口映射- Specified by:
unBindRemotePortin interfaceSession- Parameters:
remoteAddress- 需要解除的远程地址和端口
-
exec
执行Shell命令(使用EXEC方式)此方法单次发送一个命令到服务端,不读取环境变量,不会产生阻塞。
- Parameters:
cmd- 命令charset- 发送和读取内容的编码errStream- 错误信息输出到的位置- Returns:
- 执行返回结果
-
execByShell
执行Shell命令此方法单次发送一个命令到服务端,自动读取环境变量,可能产生阻塞。
- Parameters:
cmd- 命令charset- 发送和读取内容的编码errStream- 错误信息输出到的位置- Returns:
- 执行返回结果
-