org/objectweb/proactive/core/ssh/SSHClient.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.ssh;
00032 
00033 import java.io.InputStream;
00034 
00035 import com.jcraft.jsch.Channel;
00036 import com.jcraft.jsch.ChannelExec;
00037 import com.jcraft.jsch.JSch;
00038 import com.jcraft.jsch.Session;
00039 
00051 public class SSHClient
00052 {
00053 
00058     private static String buildCmdLine(String[] args, int index)
00059     {
00060         String cmd = "";
00061 
00062         for (int i = index; i < args.length; i++)
00063         {
00064             cmd += " " + args[i];
00065         }
00066 
00067         return cmd;
00068     }
00069 
00070     public static void main(String[] args)
00071     {
00072 
00073         if (args.length < 2)
00074         {
00075             System.err.println("not enought arguments\n" + "usage : " + SSHClient.class.getName()
00076                                + " username@host [-p password] cmdline");
00077             System.exit(1);
00078         }
00079 
00080 //        for (int i = 0; i < args.length; i++)
00081 //        {
00082 //            System.out.println(args[i]);
00083 //        }
00084 
00085         try
00086         {
00087 
00088             String host = "";
00089             String password = "";
00090             String command = "";
00091             String user = "";
00092             int index = 0;
00093 
00094             if ("-p".equals(args[index]))
00095             {
00096                 password = args[index + 1];
00097                 index += 2;
00098             }
00099 
00100             if ("-l".equals(args[index]))
00101             {
00102                 user = args[index + 1];
00103                 index += 2;
00104             }
00105 
00106             host = args[index];
00107             index++;
00108             command = buildCmdLine(args, index);
00109 
00110             JSch jsch = new JSch();
00111 
00112             Session session = jsch.getSession(user, host, 22);
00113             session.setPassword(password);
00114 
00115             java.util.Hashtable<String, String> config = new java.util.Hashtable<String, String>();
00116             config.put("StrictHostKeyChecking", "no");
00117             session.setConfig(config);
00118 
00119             // System.out.println("---" + command + "---");
00120 
00121             session.connect(3000); // making connection with timeout.
00122 
00123             Channel channel = session.openChannel("exec");
00124             ((ChannelExec) channel).setCommand(command);
00125             channel.setXForwarding(true);
00126 
00127             channel.setInputStream(System.in);
00128             // channel.setOutputStream(System.out);
00129 
00130             ((ChannelExec) channel).setErrStream(System.err);
00131 
00132             InputStream in = channel.getInputStream();
00133 
00134             channel.connect();
00135 
00136             byte[] tmp = new byte[1024];
00137             while (true)
00138             {
00139                 while (in.available() > 0)
00140                 {
00141                     int i = in.read(tmp, 0, 1024);
00142                     if (i < 0)
00143                         break;
00144                     System.out.print(new String(tmp, 0, i));
00145                 }
00146                 if (channel.isClosed())
00147                 {
00148                     in.close();
00149                   //  System.out.println("JSCH: exit-status: " + channel.getExitStatus());
00150                     break;
00151                 }
00152                 try
00153                 {
00154                     Thread.sleep(1000);
00155                 }
00156                 catch (Exception ee)
00157                 {
00158                 }
00159             }
00160             channel.disconnect();
00161             session.disconnect();
00162 
00163         }
00164         catch (Exception e)
00165         {
00166             e.printStackTrace();
00167         }
00168         System.exit(0);
00169     }
00170 
00171 }
00172 

Generated on Mon Jan 22 15:16:09 2007 for ProActive by  doxygen 1.5.1