/************************************************************************/ 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; } /************************************************************************/
/** Disconnect * @param ssh */ public static void disConnect(SshClient ssh) { if(ssh.isConnected()){ ssh.disconnect(); } }
private boolean openSession() throws IOException { open(); /** * open a new channel */ session = ssh.openSftpChannel(); return session != null; }
/***********************************************************************/ 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); } } /***********************************************************************/
/** * 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"); } }
/************************************************************************/ 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; } /***********************************************************************/
String commandOutput = ""; SshClient ssh = connect(host, port,username, password); SessionChannelClient sessionChannel = ssh.openSessionChannel(); sessionChannel.getState().waitForState(ChannelState.CHANNEL_CLOSED); br.close(); ssh.disconnect(); return commandOutput; ssh.disconnect(); return commandOutput;
/***********************************************************************/ private void closeSsh() { ssh.disconnect(); ssh = null; } /***********************************************************************/
/** * Create an SFTP connection to a client * * @param host the hostname of the client * @param port the port the user want to use for connection * @param username the username required for authentication * @param password the password required for authentication * @return the client * @throws IOException Signals that an I/O exception has occurred. */ public static SftpClient getSftpClient(String host,int port, String username,String password) throws IOException { SshClient ssh = connect(host, port,username, password); return ssh.openSftpClient(); }
private boolean openSession() throws IOException { open(); /** * open a new channel */ session = ssh.openSessionChannel(); /** * emulate a terminal */ session.requestPseudoTerminal("ansi", 132, 24, 132, 24, ""); return session.startShell(); }
/***********************************************************************/ 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(); } /************************************************************************/
@Override public void close() throws IOException { ssh.disconnect(); super.close(); }