Jsch集成hutool进行整合测试

概述

  Jsch集成hutool进行整合测试

实践

引入jar

  引入两个包 jschhutool-all

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.31</version>
</dependency>

主要代码

  主要测试了命令执行情况,exec 是没有问题的,但 execByShell 执行出现了阻塞,不会出结果,这个问题在下面问题点章节详细说明。

import cn.hutool.extra.ssh.JschUtil;
import com.jcraft.jsch.Session;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;


public class HutoolTest {

    @Test
    public void test() {
        // 获得一个SSH会话,重用已经使用的会话
        Session session = JschUtil.getSession("10.32.36.57", 22, "root", "luxshare");
        System.out.println(JschUtil.exec(session, "ls -a ", StandardCharsets.UTF_8));
    }

    /**
     * 执行失败
     */
    @Test
    public void test2() {
        // 获得一个SSH会话,重用已经使用的会话
        Session session = JschUtil.getSession("10.32.36.57", 22, "root", "luxshare");
        System.out.println(JschUtil.execByShell(session,"ls", StandardCharsets.UTF_8));
    }

    /**
     * 解决 echo 无返回值问题
     * source /etc/profile;source ~/.bashrc;
     */
    @Test
    public void test3() {
        // 获得一个SSH会话,重用已经使用的会话
        Session session = JschUtil.getSession("10.32.36.57", 22, "root", "luxshare");
        System.out.println(JschUtil.exec(session, "source /etc/profile;source ~/.bashrc;  echo $JAVA_HOME", StandardCharsets.UTF_8));
//        System.out.println(JschUtil.execByShell(session,"ls", StandardCharsets.UTF_8));
    }


    @Test
    public void test4() {
        // 获得一个SSH会话,重用已经使用的会话
        Session session = JschUtil.getSession("10.32.36.57", 22, "root", "luxshare");
        System.out.println(JschUtil.exec(session, "ls -l ", StandardCharsets.UTF_8));
    }
}

问题

  针对 execByShell 执行出现了阻塞,hutool实际上并未给出对应的解决方案

链接
在这里插入图片描述

链接
在这里插入图片描述
根据社区推荐的解决方案,试了一下,也是不行,好在,能执行各种命令,就已经可以解决个人遇到的问题。

结束

  针对java 远程操作服务器,还有 sshj 等。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在SpringBoot中集成JSch,你可以通过引入JSch库的Maven依赖来实现。引用提供了一个示例环境,其中包括了所需的JDK版本、Maven版本以及SpringBoot版本以及JSch版本。 你可以在pom.xml文件中添加以下依赖来引入JSch库: ```xml <!-- https://mvnrepository.com/artifact/com.jcraft/jsch --> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency> ``` 这将添加JSch库到你的项目中,允许你在后端代码中使用JSch进行SSH连接、端口转发、文件传输等操作。可以参考引用中的Jsch百度百科介绍获取更多关于JSch的详细信息。 在SpringBoot中,你可以在后端框架中使用JSch来远程连接ECS服务器。你可以使用JSch提供的API来实现与服务器的交互。同时,可以结合其他工具或插件,如Websocket和xterm,来实现与前端的实时交互和终端显示器的渲染。引用提供了一些相关的博客地址供你参考。 因此,通过在SpringBoot项目中引入JSch依赖,并使用JSch提供的API,你可以实现SpringBoot集成JSch的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBoot整合ssh](https://blog.csdn.net/Yan_kee/article/details/127263952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [springboot + xterm.js + vue + websocket实现终端功能](https://download.csdn.net/download/qq_29777207/13584879)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流月up

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值