Sending data to stdin of remote process - closing has no effect ->remote process waits indefinitely
See original GitHub issueHi, i am trying to send some data to the stdin of the remote process via sshj.
The sending itself is not a big problem (using Command.getOutputStream()
), however some commands do not terminate until stdin is closed. In seems like it is impossible to use such commands via sshj as closing the OutputStream doe snot has any effect on the remote stdin.
I created the following simple example to demonstarte the problem.
It executes remotely the command cat
which simply prints everything on stdout it receives on stdin. Via sshj I am sending the data "Line1\nLine2\nLine3\n"
to it and I see that this works as the listening thread prints the received lines correctly.
However in the end this always end in an ConnectionException: Timeout expired
as the remote cat
process does not terminate because stdin is not closed.
What do I have to send to close the stdin on remote side after the sent data has been received and processed by cat
(note this just an example. I want to send larger data to a tar process to extract it on remote side without saving the tar file first)?
try (Session session = ssh.startSession()) {
final Command cmd = session.exec("cat");
Thread t = new Thread() {
@Override
public void run() {
try {
InputStream in = cmd.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
System.out.println("## " + line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
byte[] data = "Line1\nLine2\nLine3\n".getBytes();
OutputStream out = cmd.getOutputStream();
out.write(data);
out.flush();
out.close(); // this should close stdin on remote side but it doesn't do
// cmd.getInputStream()
cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
} catch (Exception e) {
e.printStackTrace();
} finally {
ssh.disconnect();
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Just don’t use the
-f
option oftar
. Then the created tar archive is written to stdout and you can receive it on the other side of the ssh session.@jpstotz I found one of the workarounds in PR #554 as: ssh.getTransport().write(new SSHPacket(Message.CHANNEL_EOF).putUInt32(session.getRecipient())); So that the outstream is closed, but still I don’t know how can I use that to tar a large directory and redirect it to remote host without saving it. I posted another PR #719, but no reply yet.