2016-12-14 81 views
1

我正在使用JSch在遠程Linux機器上運行一些命令。如何僅在Java中運行Linux命令時顯示結果?

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

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

但我真正想要的是命令的實際輸出,即/u02/app2/bbisit

有沒有辦法只得到命令的實際結果,而不是其他垃圾。

謝謝。

回答