You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "Lyor Goldstein (Jira)" <ji...@apache.org> on 2022/07/07 05:14:00 UTC

[jira] [Comment Edited] (SSHD-1276) Add support for merged inverted output and error streams of remote process

    [ https://issues.apache.org/jira/browse/SSHD-1276?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17563576#comment-17563576 ] 

Lyor Goldstein edited comment on SSHD-1276 at 7/7/22 5:13 AM:
--------------------------------------------------------------

You seem to want to read from the combined stream while the command is being executed - that is why I assume you are using the inverted streams. This is not possible due to various reasons. I recommend using the 1st option combined with a {{PipedOutputStream}}:
{code:java}
PipedInputStream mergedInput = new PipedInputStream()
PipedOutputStream mergedOutputStream = new PipedOutputStream(mergedInput);
channelExec.setOut(mergedOutputStream);
channelExec.setErr(mergedOutputStream);
channelExec.open.verify();

...read from mergedInput...
{code}

However, it is much more complicated than this - you need to make sure that you start reading from the input stream +before+ opening the channel otherwise the "pump" thread might block - and that means spawning a thread and killing it once execution is done or there is an exception. Moreover, you need to make sure that the data from the STDOUT does not intermingle with that of STDERR since they may originate from different threads.

This is doable - up to you. However, if all you want is to run a command and then examine its output afterwards I suggest you look into the {{ClientSession#executeRemoteCommand}} method.

I have converted this into a new feature - perhaps we might get to it some day




was (Author: lgoldstein):
You seem to want to read from the combined stream while the command is being executed - that is why I assume you are using the inverted streams. This is not possible due to various reasons. I recommend using the 1st option combined with a {{PipedOutputStream}}:
{code:java}
PipedInputStream mergedInput = new PipedInputStream()
PipedOutputStream mergedOutputStream = new PipedOutputStream(mergedInput);
channelExec.setOut(mergedOutputStream);
channelExec.setErr(mergedOutputStream);
channelExec.open.verify();

...read from mergedInput...
{code}

However, it is much more complicated than this - you need to make sure that you start reading from the input stream +before+ opening the channel otherwise the "pump" thread might block - and that means spawning a thread and killing it once execution is done or there is an exception. Moreover, you need to make sure that the data from the STDOUT does not intermingle with that of STDERR since they may originate from different threads.

This is doable - up to you. However, if all you want is to run a command and then examine its output afterwards I suggest you look into the {{ClientSession#executeRemoteCommand}} method



> Add support for merged inverted output and error streams of remote process
> --------------------------------------------------------------------------
>
>                 Key: SSHD-1276
>                 URL: https://issues.apache.org/jira/browse/SSHD-1276
>             Project: MINA SSHD
>          Issue Type: New Feature
>    Affects Versions: 2.8.0
>         Environment: Java SE 8, Apache NetBeans IDE 8.2
>            Reporter: dgü
>            Priority: Minor
>
> Hello!
> I want to merge output and error streams of remote process as {{java.lang.ProcessBuilder#redirectErrorStream(true)}} does.
> If I set output and error streams;
> {quote}channelExec.setOut(mergedOutputStream);
> channelExec.setErr(mergedOutputStream);
> channelExec.open.verify();
> {quote}
> Then, {{ChannelExec#getInvertedOut()}} and {{ChannelExec#getInvertedErr()}} return {{{}null{}}}.
> If I don't set output and error streams;
> {quote}//channelExec.setOut(mergedOutputStream);
> //channelExec.setErr(mergedOutputStream);
> channelExec.open.verify();
> {quote}
> Then, {{ChannelExec#getInvertedOut()}} and {{ChannelExec#getInvertedErr()}} return not {{{}null{}}}. But, streams are not merged.
> How can I merge output and error streams of remote process ?
> Thanks in advance!



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org