This commit is contained in:
Looly
2023-09-24 17:16:21 +08:00
parent ed0b9a16ef
commit 9461336639
10 changed files with 568 additions and 244 deletions

View File

@@ -16,7 +16,7 @@ import org.apache.commons.net.ftp.FTPSClient;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.extra.ssh.engine.jsch.Sftp;
import org.dromara.hutool.extra.ssh.engine.jsch.JschSftp;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -94,7 +94,7 @@ public class FtpTest {
@Test
@Disabled
public void recursiveDownloadFolderSftp() {
final Sftp ftp = new Sftp("127.0.0.1", 22, "test", "test");
final JschSftp ftp = new JschSftp("127.0.0.1", 22, "test", "test");
ftp.cd("/file/aaa");
Console.log(ftp.pwd());
@@ -131,7 +131,7 @@ public class FtpTest {
@Test
@Disabled
public void existSftpTest() {
try (final Sftp ftp = new Sftp("127.0.0.1", 22, "test", "test")) {
try (final JschSftp ftp = new JschSftp("127.0.0.1", 22, "test", "test")) {
Console.log(ftp.pwd());
Console.log(ftp.exist(null));
Console.log(ftp.exist(""));

View File

@@ -14,9 +14,9 @@ package org.dromara.hutool.extra.ssh;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.lang.Console;
import com.jcraft.jsch.Session;
import org.dromara.hutool.extra.ssh.engine.jsch.JschUtil;
import org.dromara.hutool.extra.ssh.engine.jsch.Sftp;
import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.extra.ssh.engine.jsch.JschSession;
import org.dromara.hutool.extra.ssh.engine.jsch.JschSftp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -27,25 +27,26 @@ import org.junit.jupiter.api.Test;
* @author looly
*
*/
public class JschUtilTest {
public class JschTest {
@SuppressWarnings("resource")
@Test
@Disabled
public void bindPortTest() {
//新建会话此会话用于ssh连接到跳板机堡垒机此处为10.1.1.1:22
final Session session = JschUtil.getSession("looly.centos", 22, "test", "123456");
final JschSession session = new JschSession(new Connector("looly.centos", 22, "test", "123456"));
// 将堡垒机保护的内网8080端口映射到localhost我们就可以通过访问http://localhost:8080/访问内网服务了
JschUtil.bindPort(session, "172.20.12.123", 8080, 8080);
session.bindLocalPort("172.20.12.123", 8080, 8080);
}
@SuppressWarnings("resource")
@Test
@Disabled
public void bindRemotePort() {
// 建立会话
final Session session = JschUtil.getSession("looly.centos", 22, "test", "123456");
final JschSession session = new JschSession(new Connector("looly.centos", 22, "test", "123456"));
// 绑定ssh服务端8089端口到本机的8000端口上
final boolean b = JschUtil.bindRemotePort(session, 8089, "localhost", 8000);
final boolean b = session.bindRemotePort(8089, "localhost", 8000);
Assertions.assertTrue(b);
// 保证一直运行
}
@@ -54,43 +55,38 @@ public class JschUtilTest {
@Test
@Disabled
public void sftpTest() {
final Session session = JschUtil.getSession("looly.centos", 22, "root", "123456");
final Sftp sftp = JschUtil.createSftp(session);
sftp.mkDirs("/opt/test/aaa/bbb");
final JschSession session = new JschSession(new Connector("looly.centos", 22, "root", "123456"));
final JschSftp jschSftp = session.openSftp(CharsetUtil.UTF_8);
jschSftp.mkDirs("/opt/test/aaa/bbb");
Console.log("OK");
}
@SuppressWarnings("CallToPrintStackTrace")
@Test
@Disabled
public void reconnectIfTimeoutTest() throws InterruptedException {
final Session session = JschUtil.getSession("sunnyserver", 22,"mysftp","liuyang1234");
final Sftp sftp = JschUtil.createSftp(session);
final JschSession session = new JschSession(new Connector("sunnyserver", 22,"mysftp","liuyang1234"));
final JschSftp jschSftp = session.openSftp(CharsetUtil.UTF_8);
Console.log("打印pwd: " + sftp.pwd());
Console.log("cd / : " + sftp.cd("/"));
Console.log("打印pwd: " + jschSftp.pwd());
Console.log("cd / : " + jschSftp.cd("/"));
Console.log("休眠一段时间,查看是否超时");
Thread.sleep(30 * 1000);
try{
// 当连接超时时isConnected()仍然返回truepwd命令也能正常返回因此利用发送cd命令的返回结果来判断是否连接超时
Console.log("isConnected " + sftp.getClient().isConnected());
Console.log("打印pwd: " + sftp.pwd());
Console.log("cd / : " + sftp.cd("/"));
Console.log("isConnected " + jschSftp.getClient().isConnected());
Console.log("打印pwd: " + jschSftp.pwd());
Console.log("cd / : " + jschSftp.cd("/"));
}catch (final SshException e) {
e.printStackTrace();
}
Console.log("调用reconnectIfTimeout方法判断是否超时并重连");
sftp.reconnectIfTimeout();
jschSftp.reconnectIfTimeout();
Console.log("打印pwd: " + sftp.pwd());
Console.log("打印pwd: " + jschSftp.pwd());
IoUtil.closeQuietly(sftp);
}
@Test
@Disabled
public void getSessionTest(){
JschUtil.getSession("192.168.1.134", 22, "root", "aaa", null);
IoUtil.closeQuietly(jschSftp);
}
}