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 a command. You should be asked for 037 * password at most once, if the @user.dir@/.ssh/id_dsa private key does not 038 * exist or is not valid for the selected host. Timeout is set to 1 minute, so 039 * if your command is longer, it will be killed. 040 * 041 * Arguments: user@host command1 string (enclose in "" if contains space) 042 */ 043public class TestExecThirdparty { 044 045 private static SshExec ssh; 046 private static String user; 047 private static String host; 048 049 public static void main(String[] arg) throws SshException, 050 InterruptedException { 051 052 System.out.println("arg length = " + arg.length); 053 String target = arg.length > 0 ? arg[0] : "pnb@localhost:22"; 054 String thirdparty = arg.length > 1 ? arg[1] 055 : "pnorbert@malabar.cs.ucdavis.edu"; 056 String command = arg.length > 2 ? arg[2] : "scp " + thirdparty 057 + ":.bashrc bashrc." + thirdparty; 058 059 System.out.println("remote machine = " + target); 060 System.out.println("third party machine = " + thirdparty); 061 System.out.println("command = " + command); 062 063 ssh = new SshExec(target); 064 File iddsa = new File(System.getProperty("user.home") + File.separator 065 + ".ssh" + File.separator + "id_dsa"); 066 if (iddsa.exists()) 067 ssh.addIdentity(iddsa); 068 File idrsa = new File(System.getProperty("user.home") + File.separator 069 + ".ssh" + File.separator + "id_rsa"); 070 if (idrsa.exists()) 071 ssh.addIdentity(idrsa); 072 ssh.openConnection(); 073 074 ssh.setTimeout(60, false, false); 075 ssh.setForcedCleanUp(true); 076 077 ByteArrayOutputStream streamOut = new ByteArrayOutputStream(); 078 ByteArrayOutputStream streamErr = new ByteArrayOutputStream(); 079 080 try { 081 int exitCode = ssh.executeCmd(command, streamOut, streamErr, 082 thirdparty); 083 084 if (exitCode != 0) 085 System.out.println("Error when making connection to " + 086 // user + "@" + host + " exit code = " + exitCode); 087 target + " exit code = " + exitCode); 088 089 System.out.println(" exit code = " + exitCode 090 + "\n ---- output stream -----\n" + streamOut 091 + " ------ error stream -------\n" + streamErr 092 + " ---------------------------\n"); 093 } catch (ExecException e) { 094 System.out.println("Error: " + e); 095 } 096 ssh.closeConnection(); 097 System.exit(0); 098 099 } 100 101}