我正在使用JSCH调用位于远程计算机上的脚本文件(.sh)。在执行期间,脚本输出错误和成功语句。我写的JSCH代码展示了两个流,InputStream&错误流
如何获得包含错误和输出的单个输入流?
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
答案 0 :(得分:3)
初始化要写入的输出流,然后使用setOutputSteam和setErrStream
而不是getInputStreamOutputStream out = new OutputStream();
channel.setOutputStream(out);
channel.setErrStream(out);
请注意,当通道断开连接时,“out”将关闭。要防止该行为,请在设置输出流时添加布尔值:
channel.setErrStream(out, true);
channel.setOutputSteam(out, true);
如果您在JSCH ChannelExec会话中使用的输出流正在代码中的其他位置重用,这可能很重要。
如果您需要将输出流读入输入流,请参阅this question.