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

笑着哭i 提交于 2019-12-13 19:43:15

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/21311284/how-to-get-one-stream-from-error-stream-and-input-stream-when-calling-a-script-u

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!