/***********************************************************************/ public static void sftpUpload(FTPConfig config, File file, String remoteFileName) throws IOException { SshClient ssh = new SshClient(); SftpClient sftp = sshLogin(config, ssh); sftp.mkdirs(remoteFileName.substring(0, remoteFileName.lastIndexOf("/"))); sftp.put(new FileInputStream(file), remoteFileName); sftp.quit(); ssh.disconnect(); } /************************************************************************/
/************************************************************************/ public static File sftpDownload(FTPConfig config, File file, String remoteFileName) throws IOException { SshClient ssh = new SshClient(); SftpClient sftp = sshLogin(config, ssh); sftp.get(remoteFileName, new FileOutputStream(file)); sftp.quit(); ssh.disconnect(); return file; } /***********************************************************************/
/** * Connect. This action simulates all the actions required for a SSH connection * * @param host the hostname of the host you want to connect to * @param port the port of the host you want to connect to * @param username the username required for authentication * @param password the password required for authentication * @return the ssh client you connected to * @throws IOException Signals that an I/O exception has occurred. */ public static SshClient connect(String host, int port,String username,String password) throws IOException { SSH_LOG.info("Connecting to " + host); SshClient ssh = new SshClient(); ssh.connect(host, port, new IgnoreHostKeyVerification()); PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient(); passwordAuthenticationClient.setUsername(username); passwordAuthenticationClient.setPassword(password); int result = ssh.authenticate(passwordAuthenticationClient); if (result != AuthenticationProtocolState.COMPLETE) { throw new IOException("Login to " + host + ":" + port + " "+ username + "/" + password + " failed"); } SSH_LOG.info("Connected " + host); return ssh; }
/***********************************************************************/ public synchronized Connection makeConnection(String database, DatabaseConfiguration originalConfiguration) { int port = counter++; DatabaseConfiguration config = new DatabaseConfiguration(originalConfiguration.getDataSourceName(), originalConfiguration.getDriver(), originalConfiguration.getProtocol(), "localhost", "" + port, database, originalConfiguration.getUserName(), originalConfiguration.getPassword(), originalConfiguration.getType()); try { LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); SshClient ssh = new SshClient(); ssh.setSocketTimeout(60000); ssh.connect(originalConfiguration.getServer(), new IgnoreHostKeyVerification()); PasswordAuthenticationClient pwd = new PasswordAuthenticationClient(); pwd.setUsername(originalConfiguration.getUserName()); pwd.setPassword(originalConfiguration.getPassword()); ssh.authenticate(pwd); ForwardingClient client = ssh.getForwardingClient(); client.addLocalForwarding(config.getProtocol(), "0.0.0.0", config.getPort(), "localhost", originalConfiguration.getPort()); client.startLocalForwarding(config.getProtocol()); return new SshConnection(ssh, config.makeConnection()); } catch (IOException ie) { throw ObjectUtils.throwAsError(ie); } } /***********************************************************************/