question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jpstotzcommented, Sep 8, 2021

Just don’t use the -f option of tar. Then the created tar archive is written to stdout and you can receive it on the other side of the ssh session.

0reactions
kholoudasemcommented, Sep 8, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending data to stdin of remote process - closing has no effect ...
Sending data to stdin of remote process - closing has no effect ->remote process waits indefinitely.
Read more >
linux - Can I send some text to the STDIN of an active process ...
The only way to make it do a graceful shutdown is to go to the screen process, switch to the window it's running...
Read more >
exec.Wait() with a modified Stdin waits indefinitely
In order for stdin to act like a stream, the os/exec package needs to launch a goroutine to copy the data between the...
Read more >
Process running in SSH-allocated TTY not terminating once ...
Some theory. The best I can figure out is that the local EOF is not being propagated to the remote process. Basically yes,...
Read more >
Why does my Python background process end when SSH ...
The process ends as soon as ssh has finished running it and closes the session. What is the difference between the two? EDIT:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found