src/com/esotericsoftware/scar/Scar.java
static public void sftpUpload ( //
		String server1, int port1, String user1, final String password1, //
		String server2, int port2, String user2, final String password2, //
		String dir, Paths paths, final ProgressMonitor monitor, final boolean printProgress) throws IOException {

		Session session1 = null, session2 = null, session = null;
		try {
			long total = 0;
			for (String path : paths)
				total += new File(path).length();

			if (TRACE) {
				JSch.setLogger(new com.jcraft.jsch.Logger() {
					public boolean isEnabled (int pLevel) {
						return true;
					}

					public void log (int level, String message) {
						trace("scar", message);
					}
				});
			}

			JSch jsch = new JSch();
			ChannelSftp channel = null;
			boolean reconnecting = false, cd = true;
			for (String path : paths) {
				SftpFileUpload fileUpload = new SftpFileUpload(path, total, monitor, printProgress);
				if (INFO) info("scar", "SFTP upload: " + fileUpload.file.getName() + " -> " + dir);
				while (true) {
					// Connect.
					try {
						if (session1 == null || !session1.isConnected() || (server2 != null && !session2.isConnected())) {
							if (session1 != null) session1.disconnect();
							if (session2 != null) session2.disconnect();
							channel = null;

							session1 = jsch.getSession(user1, server1, port1);
							session1.setPassword(password1);
							session1.setConfig("StrictHostKeyChecking", "no");
							session1.connect(10000);

							if (server2 != null) {
								int forwardPort = session1.setPortForwardingL(0, server2, port2);
								session2 = jsch.getSession(user2, "127.0.0.1", forwardPort);
								session2.setPassword(password2);
								session2.setConfig("StrictHostKeyChecking", "no");
								session2.setHostKeyAlias(server2);
								session2.connect(10000);
								session = session2;
							} else
								session = session1;
						}
						if (channel == null || !channel.isConnected()) {
							channel = (ChannelSftp)session.openChannel("sftp");
							channel.connect(10000);
							reconnecting = false;
							cd = true;
						}
					} catch (Exception ex) {
						if (TRACE) trace("scar", "Connection error.", ex);
						if (!reconnecting) {
							reconnecting = true;
							if (WARN) warn("scar", "Connecting...");
						}
						try {
							Thread.sleep(250);
						} catch (Exception ignored) {
						}
						continue;
					}

					// Upload.
					try {
						if (cd) {
							fileUpload.cd(channel, dir);
							cd = false;
						}
						fileUpload.upload(channel);
						break;
					} catch (FileNotFoundException ex) {
						throw ex;
					} catch (Exception ex) {
						if (TRACE) trace("scar", "Error during upload.", ex);
						continue;
					}
				}
			}
		} catch (Exception ex) {
			throw new IOException(ex);
		} finally {
			if (session2 != null) session2.disconnect();
			if (session1 != null) session1.disconnect();
		}
	}
/** Upload through an SSH tunnel using an intermediate server.
	 * <p>
	 * Note some users have reported needing to use IP addresses. */
Upload through an SSH tunnel using an intermediate server.

Note some users have reported needing to use IP addresses.
Upload through an SSH tunnel using an intermediate server . Note some users have reported needing to use IP addresses .
static public void sftpUpload ( 
		String server1, int port1, String user1, final String password1, 
		String server2, int port2, String user2, final String password2, 
		String dir, Paths paths, final ProgressMonitor monitor, final boolean printProgress) throws IOException {
		Session session1 = null, session2 = null, session = null;
		try {
			long total = 0;
			for (String path : paths)
				total += new File(path).length();
			if (TRACE) {
				JSch.setLogger(new com.jcraft.jsch.Logger() {
					public boolean isEnabled (int pLevel) {
						return true;
					}
					public void log (int level, String message) {
						trace("scar", message);
					}
				});
			}
			JSch jsch = new JSch();
			ChannelSftp channel = null;
			boolean reconnecting = false, cd = true;
			for (String path : paths) {
				SftpFileUpload fileUpload = new SftpFileUpload(path, total, monitor, printProgress);
				if (INFO) info("scar", "SFTP upload: " + fileUpload.file.getName() + " -> " + dir);
				while (true) {
					 Connect.
					try {
						if (session1 == null || !session1.isConnected() || (server2 != null && !session2.isConnected())) {
							if (session1 != null) session1.disconnect();
							if (session2 != null) session2.disconnect();
							channel = null;
							session1 = jsch.getSession(user1, server1, port1);
							session1.setPassword(password1);
							session1.setConfig("StrictHostKeyChecking", "no");
							session1.connect(10000);
							if (server2 != null) {
								int forwardPort = session1.setPortForwardingL(0, server2, port2);
								session2 = jsch.getSession(user2, "127.0.0.1", forwardPort);
								session2.setPassword(password2);
								session2.setConfig("StrictHostKeyChecking", "no");
								session2.setHostKeyAlias(server2);
								session2.connect(10000);
								session = session2;
							} else
								session = session1;
						}
						if (channel == null || !channel.isConnected()) {
							channel = (ChannelSftp)session.openChannel("sftp");
							channel.connect(10000);
							reconnecting = false;
							cd = true;
						}
					} catch (Exception ex) {
						if (TRACE) trace("scar", "Connection error.", ex);
						if (!reconnecting) {
							reconnecting = true;
							if (WARN) warn("scar", "Connecting...");
						}
						try {
							Thread.sleep(250);
						} catch (Exception ignored) {
						}
						continue;
					}
					 Upload.
					try {
						if (cd) {
							fileUpload.cd(channel, dir);
							cd = false;
						}
						fileUpload.upload(channel);
						break;
					} catch (FileNotFoundException ex) {
						throw ex;
					} catch (Exception ex) {
						if (TRACE) trace("scar", "Error during upload.", ex);
						continue;
					}
				}
			}
		} catch (Exception ex) {
			throw new IOException(ex);
		} finally {
			if (session2 != null) session2.disconnect();
			if (session1 != null) session1.disconnect();
		}
	}
static public void sftpUpload ( String server1 , int port1 , String user1 , final String password1 , String server2 , int port2 , String user2 , final String password2 , String dir , Paths paths , final ProgressMonitor monitor , final boolean printProgress ) throws IOException { Session session1 = null , session2 = null , session = null ; try { long total = 0 ; for ( String path : paths ) total += new File ( path ) . length ( ) ; if ( TRACE ) { JSch . setLogger ( new com . jcraft . jsch . Logger ( ) { public boolean isEnabled ( int pLevel ) { return true ; } public void log ( int level , String message ) { trace ( "scar" , message ) ; } } ) ; } JSch jsch = new JSch ( ) ; ChannelSftp channel = null ; boolean reconnecting = false , cd = true ; for ( String path : paths ) { SftpFileUpload fileUpload = new SftpFileUpload ( path , total , monitor , printProgress ) ; if ( INFO ) info ( "scar" , "SFTP upload: " + fileUpload . file . getName ( ) + " -> " + dir ) ; while ( true ) { try { if ( session1 == null || ! session1 . isConnected ( ) || ( server2 != null && ! session2 . isConnected ( ) ) ) { if ( session1 != null ) session1 . disconnect ( ) ; if ( session2 != null ) session2 . disconnect ( ) ; channel = null ; session1 = jsch . getSession ( user1 , server1 , port1 ) ; session1 . setPassword ( password1 ) ; session1 . setConfig ( "StrictHostKeyChecking" , "no" ) ; session1 . connect ( 10000 ) ; if ( server2 != null ) { int forwardPort = session1 . setPortForwardingL ( 0 , server2 , port2 ) ; session2 = jsch . getSession ( user2 , "127.0.0.1" , forwardPort ) ; session2 . setPassword ( password2 ) ; session2 . setConfig ( "StrictHostKeyChecking" , "no" ) ; session2 . setHostKeyAlias ( server2 ) ; session2 . connect ( 10000 ) ; session = session2 ; } else session = session1 ; } if ( channel == null || ! channel . isConnected ( ) ) { channel = ( ChannelSftp ) session . openChannel ( "sftp" ) ; channel . connect ( 10000 ) ; reconnecting = false ; cd = true ; } } catch ( Exception ex ) { if ( TRACE ) trace ( "scar" , "Connection error." , ex ) ; if ( ! reconnecting ) { reconnecting = true ; if ( WARN ) warn ( "scar" , "Connecting..." ) ; } try { Thread . sleep ( 250 ) ; } catch ( Exception ignored ) { } continue ; } try { if ( cd ) { fileUpload . cd ( channel , dir ) ; cd = false ; } fileUpload . upload ( channel ) ; break ; } catch ( FileNotFoundException ex ) { throw ex ; } catch ( Exception ex ) { if ( TRACE ) trace ( "scar" , "Error during upload." , ex ) ; continue ; } } } } catch ( Exception ex ) { throw new IOException ( ex ) ; } finally { if ( session2 != null ) session2 . disconnect ( ) ; if ( session1 != null ) session1 . disconnect ( ) ; } }
Upload through an SSH tunnel using an intermediate server.
Upload through an SSH tunnel using an intermediate server .
// // // // Connect. // Upload.