001/* 002 * Copyright (c) 2004-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.ssh; 031 032import java.io.ByteArrayOutputStream; 033import java.io.File; 034 035/** 036 * This test connects to a host and executes two commands at once within the 037 * same session. Moreover, it repeats each command N times. You should be asked 038 * for password at most once, if the @user.dir@/.ssh/id_dsa private key does not 039 * exist or is not valid for the selected host. 040 * 041 * Arguments: user@host command1 string (enclose in "" if contains space) 042 * command2 string (enclose in "" if contains space) number of iteration 043 */ 044public class TestExecMulti { 045 046 private static SshExec ssh; 047 private static SshTest sshtest1; 048 private static SshTest sshtest2; 049 private static String user; 050 private static String host; 051 052 public static void main(String[] arg) throws SshException, 053 InterruptedException { 054 055 System.out.println("arg length = " + arg.length); 056 String target = arg.length > 0 ? arg[0] : "pnorbert@localhost"; 057 String command = arg.length > 1 ? arg[1] : "ls"; 058 String command2 = arg.length > 2 ? arg[2] : "w"; 059 String sIteration = arg.length > 3 ? arg[3] : "1"; 060 int iteration = Integer.parseInt(sIteration); 061 062 System.out.println("remote machine = " + target + "\ncommand1 = " 063 + command + "\ncommand2 = " + command2 + "\niteration = " 064 + iteration); 065 066 int atPos = target.indexOf('@'); 067 if (atPos >= 0) 068 user = target.substring(0, target.indexOf('@')); 069 else 070 user = System.getProperty("user.name"); 071 072 host = target.substring(atPos + 1); 073 074 ssh = new SshExec(user, host); 075 ssh.addIdentity(System.getProperty("user.home") + File.separator 076 + ".ssh" + File.separator + "id_dsa"); 077 ssh.openConnection(); 078 079 sshtest1 = new SshTest(command, 1, iteration); 080 sshtest2 = new SshTest(command2, 2, iteration); 081 082 // sshtest1.run(); 083 // sshtest2.run(); 084 Thread t1 = new Thread(sshtest1); 085 Thread t2 = new Thread(sshtest2); 086 087 t1.start(); 088 t2.start(); 089 t1.join(); 090 t2.join(); 091 092 ssh.closeConnection(); 093 System.exit(0); 094 095 } 096 097 public static class SshTest implements Runnable { 098 String command; 099 int id; 100 int iteration; 101 102 public SshTest(String command, int id, int iteration) { 103 this.id = id; 104 this.command = command; 105 this.iteration = iteration; 106 } 107 108 public void run2() { 109 for (int i = 1; i <= iteration; i++) { 110 System.out.println(" " + id + ": Iteration " + i 111 + " command: " + command); 112 113 String streamOut = new String(" The output is " + command 114 + "."); 115 String streamErr = new String(" The stderr is " + command 116 + "."); 117 118 System.out.println(" " + id + ":---- output stream -----\n" 119 + streamOut + "\n ----- error stream ------\n" 120 + streamErr + "\n ---------------------------\n"); 121 System.out.println(""); 122 try { 123 Thread.sleep(1000L - id * 200); 124 } catch (Exception ex) { 125 ; 126 } 127 } 128 } 129 130 public void run() { 131 132 for (int i = 1; i <= iteration; i++) { 133 System.out.println(" " + id + ": Iteration " + i 134 + " command: " + command); 135 136 ByteArrayOutputStream streamOut = new ByteArrayOutputStream(); 137 ByteArrayOutputStream streamErr = new ByteArrayOutputStream(); 138 139 try { 140 int exitCode = ssh 141 .executeCmd(command, streamOut, streamErr); 142 143 if (exitCode != 0) 144 System.out.println(" " + id 145 + ":Error when making connection to " + user 146 + "@" + host + " exit code = " + exitCode); 147 148 System.out 149 .println(" " + id + ": exit code = " + exitCode 150 + " ---- output stream -----\n" + streamOut 151 + " ----- error stream ------\n" 152 + streamErr 153 + " ---------------------------\n"); 154 } catch (ExecException e) { 155 System.out.println(" " + id + ": " + e); 156 } 157 System.out.println(""); 158 } 159 } 160 161 } 162 163}