Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

java - SSHJ When executing instructions that need to be input, reading inputStream after writing to outputStream will always block

I need to execute a command, and then enter a string of characters into the console, the console receives and prints it out, and then I try to get the content it prints, but it keeps blocking and the method cannot be executed. Script executed,cmd.sh

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

Java code

        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()));
question from:https://stackoverflow.com/questions/65917744/sshj-when-executing-instructions-that-need-to-be-input-reading-inputstream-afte

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...