hierynomus / hierynomus/sshj

No content on Shell's ErrorStream

Open
#285 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.7k
Forks
620
Avg merge
3d 23h
Merged PRs (30d)
11

Description

Hi,

I am having trouble getting the stderr content of a command from `net.schmizz.sshj.connection.channel.direct.Session.Shell.getErrorStream()`: All output is written exclusively to the stdout stream retrievable via `net.schmizz.sshj.connection.channel.Channel.getInputStream()`.

From what I see in the debugger stderr-content would be written to the error stream if the received SSH message is of type CHANNEL_EXTENDED_DATA, but all output is received as CHANNEL_DATA messages. I have tried different terminal types (`"vt100"`, `"xterm"` and `"dumb"`) and setting the PTYModes `IEXTEN` and `OPOST` using `allocatePTY(...)` but could not change the behaviour.

Does somebody have suggestion on how to direct the stderr output into the Shells ErrorStream instead of it's InputStream?

The code I used is adapted from the RudimentaryPTY-example:
```
package net.schmizz.sshj.examples;

import java.io.*;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.*;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Shell;
import net.schmizz.sshj.transport.verification.*;

/** A very rudimentary psuedo-terminal based on console I/O. */
class RudimentaryPTY {
public static void main(final String... args)
throws IOException, InterruptedException {
final SSHClient ssh = new SSHClient();
final File khFile = new File(OpenSSHKnownHosts.detectSSHDir(), "known_hosts");
ssh.addHostKeyVerifier(new ConsoleKnownHostsVerifier(khFile, System.console()));
ssh.connect("localhost");
try {
ssh.authPublickey(System.getProperty("user.name"));
final Session session = ssh.startSession();
try {
session.allocateDefaultPTY();
final Shell shell = session.startShell();
final InputStream inputStream = shell.getInputStream();
new StreamCopier(inputStream, new FileOutputStream("stdout.txt"), LoggerFactory.DEFAULT)
.bufSize(session.getLocalMaxPacketSize())
.spawn("stdout");

final InputStream errorStream = shell.getErrorStream();
new StreamCopier(errorStream, new FileOutputStream("stderr.txt"), LoggerFactory.DEFAULT)
.bufSize(session.getLocalMaxPacketSize())
.spawn("stderr");

final OutputStream outputStream = shell.getOutputStream();
outputStream.write("echo \"stdout\"; >&2 echo \"stderr\"\n".getBytes());
outputStream.close();
Thread.sleep(5000l);
} finally {
session.close();
}
} finally {
ssh.disconnect();
}
}
}
```
stderr.txt is empty after the execution:
> $ ls -lah std*
> -rw-rw-r-- 1 [...] 0 Nov 30 15:00 stderr.txt
> -rw-rw-r-- 1 [...] 494 Nov 30 15:00 stdout.txt

while all content is saved in stdout.txt
> $ cat stdout.txt
> Welcome to Ubuntu 16.10 (GNU/Linux 4.8.0-27-generic x86_64)
>
> * Documentation: https://help.ubuntu.com
> * Management: https://landscape.canonical.com
> * Support: https://ubuntu.com/advantage
>
> 0 packages can be updated.
> 0 updates are security updates.
>
> *** System restart required ***
> Last login: Wed Nov 30 14:45:39 2016 from 127.0.0.1
> echo "stdout"; >&2 echo "stderr"
> [...]$ echo "stdout"; >&2 echo "stderr"
> stdout
> stderr

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.