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 TestExec { 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] : "pnorbert@localhost:22"; 054 String command = arg.length > 1 ? arg[1] : "ls"; 055 056 System.out.println("remote machine = " + target + "\ncommand = " 057 + command); 058 059 /* 060 * int atPos = target.indexOf('@'); if ( atPos >= 0) user = 061 * target.substring(0, target.indexOf('@')); else user = 062 * System.getProperty("user.name"); 063 * 064 * host=target.substring(atPos+1); 065 * 066 * ssh = new SshExec(user, host); 067 */ 068 069 ssh = new SshExec(target); 070 File iddsa = new File(System.getProperty("user.home") + File.separator 071 + ".ssh" + File.separator + "id_dsa"); 072 if (iddsa.exists()) 073 ssh.addIdentity(iddsa); 074 File idrsa = new File(System.getProperty("user.home") + File.separator 075 + ".ssh" + File.separator + "id_rsa"); 076 if (idrsa.exists()) 077 ssh.addIdentity(idrsa); 078 ssh.openConnection(); 079 080 ssh.setTimeout(60, false, false); 081 ssh.setForcedCleanUp(true); 082 083 ByteArrayOutputStream streamOut = new ByteArrayOutputStream(); 084 ByteArrayOutputStream streamErr = new ByteArrayOutputStream(); 085 086 try { 087 int exitCode = ssh.executeCmd(command, streamOut, streamErr); 088 089 if (exitCode != 0) 090 System.out.println("Error when making connection to " + 091 // user + "@" + host + " exit code = " + exitCode); 092 target + " exit code = " + exitCode); 093 094 System.out.println(" exit code = " + exitCode 095 + "\n ---- output stream -----\n" + streamOut 096 + " ----- error stream ------\n" + streamErr 097 + " ---------------------------\n"); 098 } catch (ExecException e) { 099 System.out.println("Error: " + e); 100 } 101 ssh.closeConnection(); 102 System.exit(0); 103 104 } 105 106}