00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00081
00082
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
00120
00121 session.connect(3000);
00122
00123 Channel channel = session.openChannel("exec");
00124 ((ChannelExec) channel).setCommand(command);
00125 channel.setXForwarding(true);
00126
00127 channel.setInputStream(System.in);
00128
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
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