/** * Start the application * * @throws IOException if there is a problem with connection * @throws InterruptedException if thread was interrupted */ private void start() throws IOException, InterruptedException { Connection c = new Connection(myHost.getHostName(), myHost.getPort()); try { configureKnownHosts(c); c.connect(new HostKeyVerifier()); authenticate(c); final Session s = c.openSession(); try { s.execCommand(myCommand); // Note that stdin is not being waited using semaphore. // Instead, the SSH process waits for remote process exit // if remote process exited, none is interested in stdin // anyway. forward("stdin", s.getStdin(), System.in, false); forward("stdout", System.out, s.getStdout(), true); forward("stderr", System.err, s.getStderr(), true); myForwardCompleted.acquire(2); // wait only for stderr and stdout s.waitForCondition(ChannelCondition.EXIT_STATUS, Long.MAX_VALUE); Integer exitStatus = s.getExitStatus(); if (exitStatus == null) { // broken exit status exitStatus = 1; } System.exit(exitStatus.intValue() == 0 ? myExitCode : exitStatus.intValue()); } finally { s.close(); } } finally { c.close(); } }
/** * The session for opened for this SCP transfer must be closed using SCPInputStream#close * * @param remoteFile * @return * @throws IOException */ public SCPInputStream get(final String remoteFile) throws IOException { Session sess = null; if (null == remoteFile) throw new IllegalArgumentException("Null argument."); if (remoteFile.length() == 0) throw new IllegalArgumentException("Cannot accept empty filename."); String cmd = "scp -f"; cmd += (" \"" + remoteFile + "\""); sess = conn.openSession(); sess.execCommand(cmd, charsetName); return new SCPInputStream(this, sess); }
/** * Create a SFTP v3 client. * * @param conn The underlying SSH-2 connection to be used. * @param debug * @throws java.io.IOException * @deprecated this constructor (debug version) will disappear in the future, use {@link * #SFTPv3Client(Connection)} instead. */ public SFTPv3Client(Connection conn, PrintStream debug) throws IOException { if (conn == null) throw new IllegalArgumentException("Cannot accept null argument!"); this.conn = conn; this.debug = debug; if (debug != null) debug.println("Opening session and starting SFTP subsystem."); sess = conn.openSession(); sess.startSubSystem("sftp"); is = sess.getStdout(); os = new BufferedOutputStream(sess.getStdin(), 2048); if ((is == null) || (os == null)) throw new IOException("There is a problem with the streams of the underlying channel."); init(); }
/** * The session for opened for this SCP transfer must be closed using SCPOutputStream#close * * @param remoteFile * @param length The size of the file to send * @param remoteTargetDirectory * @param mode * @return * @throws IOException */ public SCPOutputStream put( final String remoteFile, long length, String remoteTargetDirectory, String mode) throws IOException { Session sess = null; if (null == remoteFile) throw new IllegalArgumentException("Null argument."); if (null == remoteTargetDirectory) remoteTargetDirectory = ""; if (null == mode) mode = "0600"; if (mode.length() != 4) throw new IllegalArgumentException("Invalid mode."); for (int i = 0; i < mode.length(); i++) if (Character.isDigit(mode.charAt(i)) == false) throw new IllegalArgumentException("Invalid mode."); remoteTargetDirectory = (remoteTargetDirectory.length() > 0) ? remoteTargetDirectory : "."; String cmd = "scp -t -d \"" + remoteTargetDirectory + "\""; sess = conn.openSession(); sess.execCommand(cmd, charsetName); return new SCPOutputStream(this, sess, remoteFile, length, mode); }