com.sshtools.j2ssh.authentication.PasswordAuthenticationClient Java Examples

The following examples show how to use com.sshtools.j2ssh.authentication.PasswordAuthenticationClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SshUtil.java    From qaf with MIT License 6 votes vote down vote up
/**
 * connect to host
 * 
 * @return
 * @throws Exception
 */
private static SshClient connectSsh() throws Exception {
	SshClient ssh = new SshClient();

	HostKeyVerification host = new IgnoreHostKeyVerification();
	String hostStr = getBundle().getString("ssh.host");
	ssh.connect(hostStr, host);
	PasswordAuthenticationClient auth = new PasswordAuthenticationClient();
	auth.setUsername(getBundle().getString("ssh.user"));
	auth.setPassword(getBundle().getString("ssh.pwd"));
	int result = ssh.authenticate(auth);

	System.out.println("Status " + result);
	if ((result == AuthenticationProtocolState.CANCELLED) || (result == AuthenticationProtocolState.FAILED)) {
		throw new Exception("Authentication Error.");
	}
	return ssh;
}
 
Example #2
Source File: FtpSession.java    From iaf with Apache License 2.0 6 votes vote down vote up
private SshAuthenticationClient getSshAuthentication() throws Exception {
	if (StringUtils.isNotEmpty(privateKeyFilePath)) {
		PublicKeyAuthenticationClient pk = new PublicKeyAuthenticationClient();
		CredentialFactory pkcf = new CredentialFactory(getPrivateKeyAuthAlias(), getUsername(), getPrivateKeyPassword());
		pk.setUsername(pkcf.getUsername());
		SshPrivateKeyFile pkFile = SshPrivateKeyFile.parse(new File(privateKeyFilePath));
		pk.setKey(pkFile.toPrivateKey(pkcf.getPassword()));
		return pk; 
	}
		CredentialFactory usercf = new CredentialFactory(getAuthAlias(), getUsername(), getPassword());
		if (StringUtils.isNotEmpty(usercf.getPassword())) {
		PasswordAuthenticationClient pac = new PasswordAuthenticationClient();
		pac.setUsername(usercf.getUsername());
		pac.setPassword(usercf.getPassword());
		return pac;
	}
	throw new Exception("Unknown authentication type, either the password or the privateKeyFile must be filled");
}
 
Example #3
Source File: SshDatabaseWrapper.java    From ApprovalTests.Java with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example #4
Source File: SSHUtils.java    From stevia with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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;
}
 
Example #5
Source File: NetUtils.java    From ApprovalTests.Java with Apache License 2.0 5 votes vote down vote up
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;
}