Currently I can use the JSch library to execute remote commands in a SSH session like this:
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, 22);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
BufferedReader in = new BufferedReader(new InputStreamReader(channel.getInputStream()));
channel.setCommand("ls -l");
channel.connect();
StringBuilder output = new StringBuilder();
String s = null;
while((s = in.readLine()) != null){
output.append(s + "\n");
}
System.out.println(output);
The commands that returns the full value right away works fine, but some interactive commands (like "Docker run") returns values every second or so (for a couple of minutes), and the previous code only read the first value returned.
Is there any way to read the values as they are returned by the command?
EDIT
This is my code now:
public String executeCommand(String cmd) {
try {
Channel channel = session.openChannel("shell");
OutputStream inputstream_for_the_channel = channel.getOutputStream();
PrintStream commander = new PrintStream(inputstream_for_the_channel, true);
channel.setOutputStream(System.out, true);
channel.connect();
commander.println(cmd);
commander.close();
do {
Thread.sleep(1000);
} while(!channel.isEOF());
} catch(Exception e) {
e.printStackTrace();
}
return null;
}