Skip to content

Sending data to stdin of remote process - closing has no effect ->remote process waits indefinitely  #553

@jpstotz

Description

@jpstotz

Hi, 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();
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions