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.File; 033import java.util.Iterator; 034import java.util.Vector; 035 036/** Scp several local files to a remote site */ 037public class TestCopyTo { 038 039 public static void main(String[] arg) throws ExecException { 040 041 // process arguments 042 if (arg.length < 3) { 043 System.err 044 .println("Usage: ... [user@]host localFile1 [localFile2 ... localFileN] remotePath"); 045 System.err 046 .println("localFile can be a directory or file pattern as well"); 047 System.err 048 .println("If there is a directory, the copy will be recursively done"); 049 System.exit(1); 050 } 051 052 System.out.println("arg length = " + arg.length); 053 String target = arg.length > 0 ? arg[0] : "pnorbert@localhost"; 054 055 boolean recursive = false; 056 057 Vector files = new Vector(); 058 for (int i = 1; i < arg.length - 1; i++) { 059 File f = new File(arg[i]); 060 if (f.isDirectory()) 061 recursive = true; 062 // else if ( ! f.isFile() ) { 063 // System.err.println("Argument source file " + arg[i] + 064 // " is not a file"); 065 // System.exit(2); 066 // } 067 files.add(f); 068 } 069 String rpath = arg[arg.length - 1]; 070 071 // determine user and host from argument 'target' 072 String user, host; 073 int atPos = target.indexOf('@'); 074 if (atPos >= 0) 075 user = target.substring(0, target.indexOf('@')); 076 else 077 user = System.getProperty("user.name"); 078 079 host = target.substring(atPos + 1); 080 081 // print what is to be done 082 if (host.equals("local")) 083 System.out.print("cp "); 084 else 085 System.out.print("scp "); 086 Iterator it = files.iterator(); 087 while (it.hasNext()) 088 System.out.print(it.next() + " "); 089 System.out.println(target + ":" + rpath); 090 System.out.println("Recursive copy: " + recursive); 091 092 // open ssh connection 093 ExecInterface eo; 094 if (host.equals("local")) 095 eo = new LocalExec(); 096 else { 097 SshExec ssh = new SshExec(user, host); 098 ssh.addIdentity(System.getProperty("user.home") + File.separator 099 + ".ssh" + File.separator + "id_dsa"); 100 ssh.openConnection(); 101 eo = ssh; 102 } 103 104 // do copy 105 int n = eo.copyTo(files, rpath, recursive); 106 System.out.println(n + " files have been copied successfully"); 107 if (!host.equals("local")) 108 ((SshExec) eo).closeConnection(); 109 110 System.exit(0); 111 112 } 113 114}