[JSch-users] How do you capture output of a command executed on Shell?
Status: Alpha
Brought to you by:
ymnk
From: Mike V <ba...@ho...> - 2008-09-15 14:06:38
|
How do you capture output of a command executed on Shell, and how do you know if it is done executing? I need this because some programs look for variables set by the .profile (ksh). This is how I'm doing it now: -open Shell channel and send a ByteArrayInputStream that contains my command, while outputing to a tmpfile.txt -sleep for a bit and then (this is because i dont know when my command is done running.. this is a retarded way of doing it since some commands take a lot longer than others) -open another channel(Exec) and send "cat tmpfile.txt" then read the input stream -remove the temp file ********************start of sample code********** commandToRun=commandToRun + " > "+tempFile+"\n"; byte[] bytes = commandToRun.getBytes(); ByteArrayInputStream bais=new ByteArrayInputStream(bytes); try { ..... //session connection stuff ..... Channel channel=session.openChannel("shell"); ((ChannelShell)channel).setInputStream(bais); channel.connect(); try{Thread.sleep(1500);}catch(Exception ee){} Channel channel2=session.openChannel("exec"); ((ChannelExec)channel2).setCommand("cat "+tempFile+"\n"); InputStream in1=channel2.getInputStream(); channel2.connect(); //channel 3 deletes the temp file **********************end of sample code ************************** There must be a better way of doing this... What about a way to know when the command is done? Since the first channel is a shell, i dont think you can wait for is to be .isClosed() since it never is. Thank you very much for your help. Mike _________________________________________________________________ |