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
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 Summary
ConstructorsConstructorDescriptionJschSession(com.jcraft.jsch.Session raw, long timeout) Constructs aJschSessionby wrapping a raw JSchSessionand a timeout.JschSession(Connector connector) Constructs aJschSessionwith the givenConnector. -
Method Summary
Modifier and TypeMethodDescriptionvoidbindLocalPort(InetSocketAddress localAddress, InetSocketAddress remoteAddress) Binds a local address (host and port) to a remote address, creating a local port forwarding tunnel.voidbindRemotePort(InetSocketAddress remoteAddress, InetSocketAddress localAddress) Binds a port on the remote SSH server to a local address, creating a remote port forwarding tunnel.voidclose()com.jcraft.jsch.ChannelcreateChannel(ChannelType channelType) Creates an SSH channel but does not connect it.Executes a command using the 'exec' channel and returns the output.exec(String cmd, Charset charset, OutputStream errStream) Executes a command using the 'exec' channel and returns the output.execByShell(String cmd, Charset charset) Executes a command within an interactive 'shell' channel.com.jcraft.jsch.SessiongetRaw()booleanChecks if the SSH session is currently connected and active.com.jcraft.jsch.ChannelopenChannel(ChannelType channelType) Opens and connects an SSH channel of a specified type.Opens an SFTP session, returning a wrapper for SFTP operations.com.jcraft.jsch.ChannelShellOpens and connects an interactive Shell channel.voidunBindLocalPort(InetSocketAddress localAddress) Removes a local port forwarding binding for a specific local address.voidunBindRemotePort(InetSocketAddress remoteAddress) Removes a remote port forwarding binding.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
Constructs aJschSessionwith the givenConnector. This creates a new JSch session based on the provided connection and authentication information.- Parameters:
connector- TheConnectorholding connection and authentication details.
-
JschSession
public JschSession(com.jcraft.jsch.Session raw, long timeout) Constructs aJschSessionby wrapping a raw JSchSessionand a timeout.- Parameters:
raw- The raw JSchSessionto wrap.timeout- The connection timeout duration in milliseconds; 0 indicates no limit.
-
-
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:SessionChecks if the SSH session is currently connected and active.- Specified by:
isConnectedin interfaceSession- Returns:
trueif the session is connected,falseotherwise.
-
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:SessionBinds a local address (host and port) to a remote address, creating a local port forwarding tunnel. This is an extended version ofSession.bindLocalPort(int, InetSocketAddress)that allows specifying the local bind address.- Specified by:
bindLocalPortin interfaceSession- Parameters:
localAddress- The localInetSocketAddresson the client machine to bind to.remoteAddress- The remoteInetSocketAddressto forward traffic to.- Throws:
org.miaixz.bus.core.lang.exception.InternalException
-
unBindLocalPort
Description copied from interface:SessionRemoves a local port forwarding binding for a specific local address.- Specified by:
unBindLocalPortin interfaceSession- Parameters:
localAddress- The localInetSocketAddressthat was previously bound.
-
bindRemotePort
public void bindRemotePort(InetSocketAddress remoteAddress, InetSocketAddress localAddress) throws org.miaixz.bus.core.lang.exception.InternalException Description copied from interface:SessionBinds 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 toremoteAddresson the SSH server will be forwarded through the tunnel tolocalAddresson the client machine.- Specified by:
bindRemotePortin interfaceSession- Parameters:
remoteAddress- TheInetSocketAddresson the SSH server to bind.localAddress- The localInetSocketAddressto forward traffic to.- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an error occurs while binding the remote port.
-
unBindRemotePort
Description copied from interface:SessionRemoves a remote port forwarding binding.- Specified by:
unBindRemotePortin interfaceSession- Parameters:
remoteAddress- The remoteInetSocketAddressthat was previously bound.
-
createChannel
Creates an SSH channel but does not connect it.- Parameters:
channelType- The type of channel to create (e.g., shell, sftp), seeChannelType.- Returns:
- An unconnected JSch
Channelobject.
-
openShell
public com.jcraft.jsch.ChannelShell openShell()Opens and connects an interactive Shell channel.- Returns:
- A connected
ChannelShellobject.
-
openChannel
Opens and connects an SSH channel of a specified type.- Parameters:
channelType- The type of channel to open (e.g., shell, sftp), seeChannelType.- Returns:
- A connected JSch
Channelobject.
-
openSftp
Opens an SFTP session, returning a wrapper for SFTP operations.- Parameters:
charset- The character set to use for file names.- Returns:
- A
JschSftpinstance for performing SFTP operations.
-
exec
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
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- TheOutputStreamto 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
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.
-