Reputation: 998
I went through the Java APIs like SSHJ and JScH for executing commands over a remote machine. I explored Java Expect API's like ExpectIt and Expect4j. But I could not find a way to stream the output line by line from the machine to my java process. Let say I am doing a grep on a huge file in my remote machine and I want to stream the huge output, line by line , to my process instead of waiting for the whole command to complete. Is it possible ? if so , how to do it ?
Upvotes: 2
Views: 1655
Reputation: 503
For opening and reading an input stream from a remote host with SSHJ and a String filename
:
try (final RemoteFile handle = sftp.open(filename, EnumSet.of(OpenMode.READ));
final InputStream in = handle.new RemoteFileInputStream();
final InputStreamReader isr = new InputStreamReader(in);
final BufferedReader reader = new BufferedReader(isr)) {
// Do stuff, for example:
String line;
while ((line = reader.readLine()) != null) {
// more stuff
}
} catch (IOException e) {
logger.error("Couldn't open SFTP input stream for file: {}", filename, e);
}
If you're looking to stream the output of a command, it's largely identical.
try (Session session = sshClient.startSession();
final Command command = session.exec("grep temp.txt");
final InputStreamReader isr = new InputStreamReader(command.getInputStream());
final BufferedReader reader = new BufferedReader(isr)) {
// Do stuff, for example:
String line;
while ((line = reader.readLine()) != null) {
// more stuff
}
} catch (IOException e) {
logger.error("Couldn't open SFTP input stream for file: {}", filename, e);
}
This should work for Java 8 and any recent version of SSHJ (currently, 0.32.0).
Upvotes: 0
Reputation: 1222
I hope we can do this once you set the output stream to a file. In the below code my output stream is directed to "outs.txt" file in your system. While it is filling you can create a new thread start processing the contents of file. Below snippet shows how to set output stream can be set. After this code snippet you have to start a thread in order to process the output from "outs.txt" file. Happy Coding!!!
ss=new StringBuilder("grep aa.txt");
ss.append("\n");
FileWriter fw = new FileWriter("comm.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(ss.toString());
bw.flush();
fw.close();
bw.close();
InputStream is=new FileInputStream("comm.txt");
OutputStream os=new FileOutputStream("outs.txt");
channel.setInputStream(is);
channel.setOutputStream(os);
Thread.sleep(1000);
channel.connect();
Upvotes: 0