I begged and borrowed some code and tried to make somewhere I can send a command
to a server and then retrieve the results. The problem I am having is that the
results is always delayed. Note I put >>><<< to separate results:

Sent carriage return, retrieved:
[joe@flos] ~ # 

Sent "who" command, retrieved empty line and prompt:

[joe@flos] ~ # 

Sent carriage return, retrieved results I expected from previous command:
who 
bob     ttyS0        Dec 23 20:44         
joe     pts/0        Jan 14 01:57 (192.168.1.87)
joe     pts/1        Jan 14 07:16 (192.168.1.100)
[joe@flos] ~ # 

This seems like real odd behavior to me. For example, if I send a command like
"who", I will get the command prompt. But if I send an empty command (carriage
return) afterward then I will get the "who" command results. 

I first do a write to the channel outputstream:

            System.out.println(">>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<");
            stdOut.write("\n".getBytes());
            results = getstuff(in,buffer,bos,channel);
            System.out.println(results);
            stdOut.flush(); results = "";

I have a method that processes the results via means I saw in various postings:

               while (in.available() > 0) {
                    int count = in.read(buffer, 0, DEFAULT_BUFFER_SIZE);
                    if (count >= 0) {
                        bos.write(buffer, 0, count);
                    } else {
                        break;
                    }
                }

The buffer is passed back. It makes me wonder, what is in the channel
OutputStream before I start sending commands. How do I clear the buffer, I
assumed stdOut.flush() should do the trick. How do I allign the InputStream such
that it has the results of the completed command sent to the OutputStream

A more complete code snippet follows

      try {
            session = jsch.getSession(username, hostname, port);
            session.setPassword(password);
            java.util.Hashtable config = new java.util.Hashtable();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.setDaemonThread(true);
            session.connect(3 * 1000); // making a connection with timeout.
            channel = (ChannelShell)session.openChannel("shell");
            channel.setPtyType("vt102");

            Hashtable env = new Hashtable();
            channel.setEnv(env);

            channel.connect(5 * 1000);
            OutputStream stdOut = channel.getOutputStream();
            InputStream in = channel.getInputStream();

            // read command output
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            String results = "";


            System.out.println(">>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<");
            stdOut.write("\n".getBytes());
            results = getstuff(in,buffer,bos,channel);
            System.out.println(results);
            stdOut.flush(); results = "";
            
            System.out.println(">>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<");            
            stdOut.write("who \n".getBytes());
            results = getstuff(in,buffer,bos,channel);
            System.out.println(results);
            stdOut.flush(); results = "";
            
            System.out.println(">>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<");
            stdOut.write("\n".getBytes());
            results = getstuff(in,buffer,bos,channel);
            System.out.println(results);
            stdOut.flush();
          
            channel.disconnect();
        } catch (JSchException jse) {
            // TODO: Add catch code
            jse.printStackTrace();
        } catch (IOException ioe) {

            ioe.printStackTrace();
        }
    }
    
    public String getstuff(InputStream in,byte[] buffer, ByteArrayOutputStream
bos, ChannelShell channel){
      int exitStatus = 0;
      try {
            final long endTime = System.currentTimeMillis() + TIMEOUT;
            while (System.currentTimeMillis() < endTime) {
                while (in.available() > 0) {
                    int count = in.read(buffer, 0, DEFAULT_BUFFER_SIZE);
                    if (count >= 0) {
                        bos.write(buffer, 0, count);
                    } else {
                        break;
                    }
                }
                if (channel.isClosed()) {
                    exitStatus = channel.getExitStatus();
                    break;
                }
                try {
                    Thread.sleep(POLL_TIMEOUT);
                } catch (InterruptedException e) {
                    //("Ignoring interrupt.");
                }
            }
        } catch (IOException ioe) {
            // TODO: Add catch code
            ioe.printStackTrace();
        }
        String stuff = bos.toString();
        bos.reset();
        return stuff;
    }   
    
}


------------------------------------------------------------------------------
Protect Your Site and Customers from Malware Attacks
Learn about various malware tactics and how to avoid them. Understand 
malware threats, the impact they can have on your business, and how you 
can protect your company and customers by using code signing.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
JSch-users mailing list
JSch-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to