2016-12-14 5 views
1

JSchを使用してリモートLinuxボックスでコマンドを実行しています。JavaでLinuxコマンドを実行しているときの結果のみを表示する方法は?

[email protected]$ 
[email protected]$ pwd 
/u02/app2/bbisit 

しかし、私が本当にしたいすべて:

session = jsch.getSession(user, "host", 1222); 
... 
Channel shellChannel = session.openChannel("shell"); 
OutputStream ops = shellChannel.getOutputStream(); 
PrintStream ps = new PrintStream(ops, true); 

ps.println("pwd"); 
InputStream in = shellChannel.getInputStream(); 
... 
//print the char stream from 'in' using something similar to 
//http://stackoverflow.com/questions/9126142/output-the-result-of-a-bash-script# 

しかし、inputStreamから結果のプリントアウトは、ユーザープロンプト、(この場合はpwd)私の元のコマンド、および結果/u02/app2/bbisitを含め、すべてのものが含まれていますコマンドの実際の出力、すなわち/u02/app2/bbisitです。

コマンドの実際の結果だけを取得する方法はありますが、他のガーベジは取得できません。

ありがとうございました。

答えて

3

「シェル」チャネルではなく「exec」チャネルを使用します。

「exec」チャネルは、コマンドの実行を自動化するためのものです。 「シェル」チャネルは、対話型シェルを実装するためのものです。

http://www.jcraft.com/jsch/examples/Exec.java.html

関連する問題