微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。