java.lang.Object
org.miaixz.bus.extra.ssh.JschKit
Jsch (Java Secure Channel) utility class. JSch is a pure Java implementation of the SSH2 protocol, enabling
connections to SSH servers for operations like port forwarding, X11 forwarding, file transfers, and remote command
execution.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclose(com.jcraft.jsch.Channel channel) Closes the specified SSH channel if it is not null and is currently connected.static voidclose(com.jcraft.jsch.Session session) Closes the specified SSH session if it is not null and is currently connected.static com.jcraft.jsch.ChannelcreateChannel(com.jcraft.jsch.Session session, ChannelType channelType, long timeout) Creates an SSH channel but does not connect it.static com.jcraft.jsch.ChannelopenChannel(com.jcraft.jsch.Session session, ChannelType channelType, long timeout) Opens and connects an SSH channel of a specified type.static com.jcraft.jsch.SessionopenSession(Connector connector) Opens an SSH session with the specified connection details.
-
Constructor Details
-
JschKit
public JschKit()
-
-
Method Details
-
openSession
Opens an SSH session with the specified connection details. This method configures the session with the host, port, user, password, and timeout from the providedConnector. It also sets default configurations for host key checking and authentication methods.- Parameters:
connector- TheConnectorobject containing connection information (host, port, user, password, timeout).- Returns:
- An initialized JSch
Sessionobject, not yet connected. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if aJSchExceptionoccurs during session creation.
-
openChannel
public static com.jcraft.jsch.Channel openChannel(com.jcraft.jsch.Session session, ChannelType channelType, long timeout) Opens and connects an SSH channel of a specified type. This method first creates the channel and then connects it with the given timeout.- Parameters:
session- The active SSHSession.channelType- The type of channel to open (e.g., shell, sftp), as defined inChannelType.timeout- The connection timeout duration in milliseconds.- Returns:
- A connected JSch
Channelobject. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if aJSchExceptionoccurs during channel connection.
-
createChannel
public static com.jcraft.jsch.Channel createChannel(com.jcraft.jsch.Session session, ChannelType channelType, long timeout) Creates an SSH channel but does not connect it. If the session is not already connected, this method will connect it first.- Parameters:
session- The SSHSession.channelType- The type of channel to create (e.g., shell, sftp), as defined inChannelType.timeout- The session connection timeout duration in milliseconds, used if the session is not yet connected.- Returns:
- An unconnected JSch
Channelobject. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if aJSchExceptionoccurs during session connection or channel creation.
-
close
public static void close(com.jcraft.jsch.Session session) Closes the specified SSH session if it is not null and is currently connected.- Parameters:
session- The SSHSessionto close.
-
close
public static void close(com.jcraft.jsch.Channel channel) Closes the specified SSH channel if it is not null and is currently connected.- Parameters:
channel- The SSHChannelto close.
-