/** * 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; }
@Override public void open() throws IOException { if (ssh.isConnected()) return; /** * SSL connect layer */ ssh.connect( getHost(), getPort(), new LocalKnownHostsKeyVerification(File.createTempFile("xxx", "xxx"))); if (!ssh.isConnected()) { throw new IOException("Unable to connect to " + url); } PasswordAuthenticationClient pwd = new PasswordAuthenticationClient(); pwd.setUsername(getUser()); pwd.setPassword(getPassword()); int result = ssh.authenticate(pwd); if (result == AuthenticationProtocolState.FAILED) { throw new IOException("The authentication failed"); } if (result == AuthenticationProtocolState.PARTIAL) { throw new IOException("The authentication succeeded but another" + "authentication is required"); } if (result == AuthenticationProtocolState.COMPLETE) { System.out.println("The authentication is complete"); } }
/************************************************************************/ 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); } } /***********************************************************************/