Hi there,

I would like to execute some commands on a remote
server using a SSH terminal. I had a look at the
example 'Shell.java' but I am in a Win2000 command
line environment, so no java awt/swing and no
known_hosts file is available.

I found a code snippet in the mailing list, but
that does not accept unknown hosts. Is there work
around for that ?

I tried to find the way myself through the source
code, but the javadoc is a little scarce.


thanx   Mike


--- example without unknown host ---

import com.jcraft.jsch.*;

public class PlainShell {
    public static void main(String[] args) {
        try{
            String host="remotehost";
            String user="username";
            String passwd="password";
            String knownhosts="/home/????/.ssh/known_hosts";

            JSch jsch=new JSch();
            jsch.setKnownHosts(knownhosts);

            Session session=jsch.getSession(user, host);
            session.setPassword(passwd);
            session.connect();

            String[] command={ "/bin/date", "/bin/date", "/bin/date" };
            for(int i=0; i<command.length; i++){
                Channel channel=session.openChannel("exec");
                ((ChannelExec)channel).setCommand(command[i]);
                channel.setInputStream(null);
                java.io.ByteArrayOutputStream out=new java.io.ByteArrayOutputStream();
                channel.setOutputStream(out);
                channel.connect();
                while(true){
                    if(channel.isEOF())break;
                    try{Thread.sleep(1000);}catch(Exception e){}
                }
                channel.disconnect();
                System.out.print(out.toString());
            }
            session.disconnect();
        }
        catch(Exception e){ System.out.println(e); }
    }
}