The following piece of code is based on Shell and Exec. I am executing multiple 
commands. But If i do not use out.flush(), in.available() gives zero data.

what 's the wrong ?



/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
import com.jcraft.jsch.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

public class Exec{
  public static void main(String[] arg){
    try{
      JSch jsch=new JSch();  
      jsch.setKnownHosts("/home/foo/.ssh/known_hosts");

      String host=null;
      if(arg.length>0){
        host=arg[0];
      }
      else{
        host=JOptionPane.showInputDialog("Enter [EMAIL PROTECTED]",
                                         System.getProperty("user.name")+
                                         "@localhost"); 
      }
      String user=host.substring(0, host.indexOf('@'));
      host=host.substring(host.indexOf('@')+1);

      Session session=jsch.getSession(user, host, 22);

      // username and password will be given via UserInfo interface.
      UserInfo ui=new MyUserInfo();
      session.setUserInfo(ui);
      
      session.connect();

   //   String command=JOptionPane.showInputDialog("Enter command",      
"set|grep SSH");

      Channel channel=session.openChannel("shell");
      channel.setInputStream(null);
      channel.setOutputStream(null);


      InputStream in=channel.getInputStream();
      OutputStream out =channel.getOutputStream();


      channel.connect();
      byte[] tmp=new byte[1024];
        while(in.available()>0){
          int i=in.read(tmp, 0, 1024);
          if(i<0)break;
          System.out.print("INPUT 11: " + new String(tmp, 0, i));
        }
      out.write(("ls -lrt").getBytes());
      out.write(("\n").getBytes());
      out.flush();
      out.write(("pwd").getBytes());
      out.write(("\n").getBytes());
      out.flush();

      while(true){
        while(in.available()>0){
          int i=in.read(tmp, 0, 1024);
          if(i<0)break;
          System.out.print("INPUT : " + new String(tmp, 0, i));
        }
        if(channel.isClosed()){
          System.out.println("exit-status: "+channel.getExitStatus());
          break;
        }
        try{Thread.sleep(1000);}catch(Exception ee){}
      }
//      channel.disconnect();
//      session.disconnect();
    }
    catch(Exception e){
      System.out.println(e);
    }
  }


 __________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to