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


_________________________________________________________________

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to