I am using a JSCH -SSH library to execute command in "shell" channel, but unable to find a way
to do 2 things:-
1) How to find whether the command is completely executed on remote unix box ?
2) How to capture the command output in String , instead of printing it on System.out console ?
Below is my code snippet which works fine to display shell command output on system.out
NOTE :I do NOT want to use ,"exec" channel, as it starts a new process for each command and does not remember "session" variables which were exported. I must use "shell" channel.
Below is my code snippet. Any help is appreciated.Thanks for your time.
try{
String commandToRun = "ls /tmp/*.log
";
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();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…