/** * 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; }
/** * Open a ssh connection on the remote server and open a sftpclient. * * @throws IOException */ public void connectAndLog() throws IOException { this.setSocketTimeout((int) this.info.getMaxIdleTime()); try { this.connect(info.getServer(), info.getPort(), new IgnoreHostKeyVerification()); } catch (IOException ex) { final StringBuilder errorSB = new StringBuilder(); errorSB.append("Can't open a connection to ["); errorSB.append(info); errorSB.append("]. Cause : " + ex.getMessage()); throw new IOException(errorSB.toString(), ex); } // Retrieve the authentication informations final SshAuthenticationClient sshAuthenticationClient; sshAuthenticationClient = info.getSshAuthentication().getAuthenticationClient(); // Try the authentication int authenticationResult = this.authenticate(sshAuthenticationClient); if (authenticationResult != AuthenticationProtocolState.COMPLETE) { this.disconnect(); throw new IOException("Can't open a connection to [" + info + "]. Cause : Authentication failed"); } this.client = this.openSftpClient(); }
/************************************************************************/ private static SftpClient sshLogin(FTPConfig config, SshClient ssh) throws IOException { ssh.setSocketTimeout(60000); ssh.connect(config.host, new IgnoreHostKeyVerification()); PasswordAuthenticationClient pwd = new PasswordAuthenticationClient(); pwd.setUsername(config.userName); pwd.setPassword(config.password); ssh.authenticate(pwd); SftpClient sftp = ssh.openSftpClient(); return sftp; } /************************************************************************/
/***********************************************************************/ 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); } } /***********************************************************************/