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