Class JschSession
java.lang.Object
org.miaixz.bus.extra.ssh.provider.jsch.JschSession
- All Implemented Interfaces:
Closeable,AutoCloseable,org.miaixz.bus.core.lang.Wrapper<Object>,org.miaixz.bus.core.Provider,Session
Jsch Session封装
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
ConstructorsConstructorDescriptionJschSession(com.jcraft.jsch.Session raw, long timeout) 构造JschSession(Connector connector) 构造 -
Method Summary
Modifier and TypeMethodDescriptionvoidbindLocalPort(InetSocketAddress localAddress, InetSocketAddress remoteAddress) 绑定端口到本地。voidbindRemotePort(InetSocketAddress remoteAddress, InetSocketAddress localAddress) 绑定ssh服务端的serverPort端口, 到本地主机的port端口上.voidclose()com.jcraft.jsch.ChannelcreateChannel(ChannelType channelType) 创建Channel连接执行Shell命令exec(String cmd, Charset charset, OutputStream errStream) 执行Shell命令(使用EXEC方式)execByShell(String cmd, Charset charset) 执行Shell命令com.jcraft.jsch.SessiongetRaw()boolean是否连接状态com.jcraft.jsch.ChannelopenChannel(ChannelType channelType) 打开Channel连接打开SFTP会话com.jcraft.jsch.ChannelShell打开Shell连接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
-
JschSession
构造- Parameters:
connector-Connector,保存连接和验证信息等
-
JschSession
public JschSession(com.jcraft.jsch.Session raw, long timeout) 构造- Parameters:
raw-Sessiontimeout- 连接超时时常,0表示不限制
-
-
Method Details
-
getRaw
public com.jcraft.jsch.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
-
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
Description copied from interface:Session解除本地端口映射- Specified by:
unBindLocalPortin interfaceSession- Parameters:
localAddress- 需要解除的本地地址
-
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- 需要解除的远程地址和端口
-
createChannel
创建Channel连接- Parameters:
channelType- 通道类型,可以是shell或sftp等,见ChannelType- Returns:
Channel
-
openShell
public com.jcraft.jsch.ChannelShell openShell()打开Shell连接- Returns:
ChannelShell
-
openChannel
打开Channel连接- Parameters:
channelType- 通道类型,可以是shell或sftp等,见ChannelType- Returns:
Channel
-
openSftp
打开SFTP会话- Parameters:
charset- 编码- Returns:
JschSftp
-
exec
执行Shell命令- Parameters:
cmd- 命令charset- 发送和读取内容的编码- Returns:
ChannelExec
-
exec
执行Shell命令(使用EXEC方式)此方法单次发送一个命令到服务端,不读取环境变量,执行结束后自动关闭channel,不会产生阻塞。
- Parameters:
cmd- 命令charset- 发送和读取内容的编码errStream- 错误信息输出到的位置- Returns:
- 执行结果内容
-
execByShell
执行Shell命令此方法单次发送一个命令到服务端,自动读取环境变量,执行结束后自动关闭channel,不会产生阻塞。
- Parameters:
cmd- 命令charset- 发送和读取内容的编码- Returns:
ChannelExec
-