我正在使用JSCH -SSH库在“shell”通道中执行命令,但无法找到方法
做两件事: –
1)如何查找命令是否在远程unix盒上完全执行?
2)如何捕获String中的命令输出,而不是在System.out控制台上打印它?
下面是我的代码片段,可以在system.out上显示shell命令输出
注意:我不想使用“exec”通道,因为它为每个命令启动一个新进程,并且不记得导出的“会话”变量.我必须使用“shell”频道.
下面是我的代码片段.任何帮助表示赞赏.谢谢你的时间.
try{
String commandToRun = "ls /tmp/*.log \n";
if(channel.isClosed())
channel=session.openChannel("shell");
byte[] bytes = commandToRun.getBytes();
ByteArrayInputStream bais=new ByteArrayInputStream(bytes);
channel.setInputStream(bais);
InputStream ins=channel.getInputStream();
channel.connect();
channel.setOutputStream(System.out);//This prints on console. Need 2 capture in String somehow?
//in-efficient way to allow command to execute completely on remote Unix machine
//DO NOT know a better way, to know when command is executed completely
Thread.sleep(5000L);
}
catch(Exception e){
System.out.println("Exception in executeCommand() --->"+ e.getMessage());
e.printStackTrace();
}