Thanks, but that's not a shell. That's just sending commands with exec.
Any environment variables (from .profile) are not going to be loaded.

My original question was how do you read the output from session.openChannel("Shell"); That opens a kornshell instance (or bash/bourne.. depending on your setup) and it also loads any settings you might have.. (path to executables, database versions, etc)

Does anybody know? Is it even possible to see when a command is done executing on the shell? channel.isClosed() does not work in this case since the shell is still open, even after I send it ByteArrayInputStream to execute a command.

Thanks.

-Mike



Date: Wed, 17 Sep 2008 12:49:22 +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

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!



Upgrade to Hotmail Plus and share more photos with bigger attachments. Click here to find out how Click here to find out how