Module bus.extra

Class JschKit

java.lang.Object
org.miaixz.bus.extra.ssh.JschKit

public class JschKit extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    close(com.jcraft.jsch.Channel channel)
    Closes the specified SSH channel if it is not null and is currently connected.
    static void
    close(com.jcraft.jsch.Session session)
    Closes the specified SSH session if it is not null and is currently connected.
    static com.jcraft.jsch.Channel
    createChannel(com.jcraft.jsch.Session session, ChannelType channelType, long timeout)
    Creates an SSH channel but does not connect it.
    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.
    static com.jcraft.jsch.Session
    Opens an SSH session with the specified connection details.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JschKit

      public JschKit()
  • Method Details

    • openSession

      public static com.jcraft.jsch.Session openSession(Connector connector)
      Opens an SSH session with the specified connection details. This method configures the session with the host, port, user, password, and timeout from the provided Connector. It also sets default configurations for host key checking and authentication methods.
      Parameters:
      connector - The Connector object containing connection information (host, port, user, password, timeout).
      Returns:
      An initialized JSch Session object, not yet connected.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if a JSchException occurs 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 SSH Session.
      channelType - The type of channel to open (e.g., shell, sftp), as defined in ChannelType.
      timeout - The connection timeout duration in milliseconds.
      Returns:
      A connected JSch Channel object.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if a JSchException occurs 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 SSH Session.
      channelType - The type of channel to create (e.g., shell, sftp), as defined in ChannelType.
      timeout - The session connection timeout duration in milliseconds, used if the session is not yet connected.
      Returns:
      An unconnected JSch Channel object.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if a JSchException occurs 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 SSH Session to 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 SSH Channel to close.