How to get one stream from error stream and input stream when calling a script using JSCH

StackOverflow https://stackoverflow.com/questions/21311284

  •  01-10-2022
  •  | 
  •  

Domanda

I am calling a script file (.sh) located on remote machine using JSCH. During execution the script outputs both error and success statements. The JSCH code what I have written exhibits two streams, InputStream & Error Stream

How can I get an single input stream that contains error and output ?

Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand("/opt/sdp/SnapShot/bin/dumpSubscribers.ksh");;
InputStream in=channel.getInputStream();
InputStream error=((ChannelExec)channel).getErrStream();
channel.connect();

**Script output:**

\[2014-01-23 19:41:01] SnapShot: Start dumping database
szTimgExtension: sdp511 enabled
functionOfferSupport active

Failed to prepare statements: ODBC Error 'S0022', TimesTen Error 2211, ODBC rc -1
[TimesTen][TimesTen 7.0.6.8.0 ODBC Driver][TimesTen]TT2211: Referenced column O.START_SECONDS not found -- file "saCanon.c", lineno 9501, procedure "sbPtTblScanOfColRef()" [Unable to prepare statement: <Statement for getting subscriber_offer data.>.]

Database error: ODBC Error 'S0022', TimesTen Error 2211, ODBC rc -1
[TimesTen][TimesTen 7.0.6.8.0 ODBC Driver][TimesTen]TT2211: Referenced column O.START_SECONDS not found -- file "saCanon.c", lineno 9501, procedure "sbPtTblScanOfColRef()" [Unable to prepare statement: <Statement for getting subscriber_offer data.>.]

[2014-01-23 19:41:01] SnapShot: Result files:
/var/opt/fds/TT/dump//SDP1.DUMP_subscriber.v3.csv
/var/opt/fds/TT/dump//SDP1.DUMP_usage_counter.v3.csv
[2014-01-23 19:41:01] SnapShot: Finished dumping database
È stato utile?

Soluzione

Initialize an Output stream for both to write to, then instead of getInputStream, use setOutputSteam and setErrStream

OutputStream out = new OutputStream();
channel.setOutputStream(out);
channel.setErrStream(out);

Note that 'out' will be closed when the channel disconnects. To prevent that behavior, add a boolean when setting the output stream:

channel.setErrStream(out, true);
channel.setOutputSteam(out, true);

This may be important if the output stream you are using for the JSCH ChannelExec session is being reused elsewhere in your code.

If you need to read the output stream into an input stream, refer to this question.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top