Mike V wrote:
Thank you! That looks a lot better than the craziness  I was doing.
When you do createChannel() how do you send the command to the shell channel?
Hi, simplifed createChannel looks like this:

    protected Channel createChannel() throws JSchException {
        ChannelExec echannel = (ChannelExec) session.openChannel("exec"); // NOI18N
        echannel.setCommand(theCommand);
        echannel.setInputStream(null);
        echannel.setErrStream(System.err);
        echannel.connect();
        return echannel;
    }

-- Sergey

Thank you for your help

-Mike




Date: Tue, 16 Sep 2008 01:16:12 +0400
From: Sergey.Grinev@Sun.COM
Subject: Re: [JSch-users] How do you capture output of a command executed on Shell?
To: baietas@hotmail.com
CC: jsch-users@lists.sourceforge.net

You can just read from channel.getInputStream(). That's my code for running remote shell commands:

            try {
                channel = createChannel();
                InputStream is = channel.getInputStream();
                in = new BufferedReader(new InputStreamReader(is));
                out = new StringWriter();

                String line;
                while ((line = in.readLine()) != null || !channel.isClosed()) {
                    if (line != null) {
                        out.write(line + '\n');
                        out.flush();
                    }
                }
                in.close();
                is.close();
                setExitStatus(channel.getExitStatus());
            } catch (JSchException jse) {
                log.warning("Jsch failure during running " + cmd);
            } catch (IOException ex) {
                log.warning("IO failure during running " + cmd);
            } finally {
                disconnect();
            }

Mike V wrote:
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 JSch-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jsch-users



Get your information fix on your phone. With MSN Mobile you get regular news, sports and  finance updates. Try it today!