java.lang.Object
org.miaixz.bus.extra.ssh.provider.sshj.SshjSession
- All Implemented Interfaces:
Closeable,Serializable,AutoCloseable,org.miaixz.bus.core.lang.Wrapper<Object>,org.miaixz.bus.core.Provider,Session
SSHJ-based Session implementation. This class provides a wrapper around the SSHJ library's session handling, offering
functionalities for command execution, port forwarding, and SFTP integration. Project homepage:
https://github.com/hierynomus/sshj
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionSshjSession(net.schmizz.sshj.SSHClient ssh) Constructs aSshjSessionwith a givenSSHClient.SshjSession(Connector connector) Constructs aSshjSessionwith 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()exec(String cmd, Charset charset, OutputStream errStream) Executes a command using the 'exec' channel.execByShell(String cmd, Charset charset, OutputStream errStream) Executes a command within an interactive 'shell' channel.net.schmizz.sshj.connection.channel.direct.SessiongetRaw()booleanChecks if the SSH session is currently connected and active.Opens an SFTP session.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
-
SshjSession
Constructs aSshjSessionwith the givenConnector.- Parameters:
connector- TheConnectorholding connection and authentication information.
-
SshjSession
public SshjSession(net.schmizz.sshj.SSHClient ssh) Constructs aSshjSessionwith a givenSSHClient.- Parameters:
ssh- TheSSHClientinstance.
-
-
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: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
-
openSftp
Opens an SFTP session.- Parameters:
charset- The character set for file names.- Returns:
- A
SshjSftpinstance.
-
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
public void unBindLocalPort(InetSocketAddress localAddress) throws org.miaixz.bus.core.lang.exception.InternalException 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.- 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: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.
-
exec
Executes a command using the 'exec' channel. This method sends a single command, does not read environment variables, and is non-blocking.- Parameters:
cmd- The command to execute.charset- The character set for sending and reading content.errStream- The output stream for error messages.- Returns:
- The execution result.
-
execByShell
Executes a command within an interactive 'shell' channel. This method loads environment variables and may be blocking.- Parameters:
cmd- The command to execute.charset- The character set for sending and reading content.errStream- The output stream for error messages.- Returns:
- The execution result.
-