SSHJ 执行需要输入的指令时,写入outputStream后读取inputStream会一直阻塞

问题描述

我需要执行一个命令,然后在控制台输入一串字符,控制台接收并打印出来,然后我尝试获取它打印的内容,但是一直阻塞,方法无法执行. 脚本执行完毕,cmd.sh

read msg
echo $msg >>log.txt
echo $msg

Java 代码

        DefaultTerminal build = DefaultTerminal.builder()
                .host(host)
                .port(port)
                .password(password)
                .username(username)
                .build();
//        ShellResult shellResult = SSH2ClientUtil.writeExec(cmd,() ->"777",build);
//    This is the connection pool I encapsulated,the client I got here must be available
        SSHClient client = SSH2ClientUtil.getClient(build);
        Session.Command exec = client.startSession().exec(cmd);
        OutputStream outputStream = exec.getoutputStream();
        InputStream inputStream = exec.getInputStream();

//    Write scripts are not executed,these are normal
        exec.getoutputStream().write("ohhhh".getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        System.out.println("result++++"+new String(inputStream.readAllBytes(),StandardCharsets.UTF_8.toString()));

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)