[英]How to show only the result when running Linux command in Java?
我正在使用JSch在遠程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
:
bbisit@sdvdbres016$
bbisit@sdvdbres016$ pwd
/u02/app2/bbisit
但我真正想要的只是命令的實際輸出,即/u02/app2/bbisit
。
有沒有辦法只獲得命令的實際結果,而不是其他垃圾。
使用“exec”通道,而不是“shell”通道。
“exec”通道用於自動執行命令。 “shell”通道用於實現交互式shell。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.