Java Code Examples for net.schmizz.sshj.SSHClient#authPassword()

The following examples show how to use net.schmizz.sshj.SSHClient#authPassword() . 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: SSHChannel.java    From TeamSpeak-3-Java-API with MIT License 6 votes vote down vote up
SSHChannel(TS3Config config) throws IOException {
	if (!config.hasLoginCredentials()) {
		throw new TS3ConnectionFailedException("Anonymous queries are not supported when using SSH.\n" +
				"\t\tYou must specify a query username and password using TS3Config#setLoginCredentials.");
	}

	try {
		client = new SSHClient();
		File knownHostsFile = new File(OpenSSHKnownHosts.detectSSHDir(), KNOWN_HOSTS_FILE_NAME);
		client.addHostKeyVerifier(new AutoAddKnownHosts(knownHostsFile));
		client.setConnectTimeout(config.getCommandTimeout());
		client.setTimeout(config.getCommandTimeout());
		client.setRemoteCharset(StandardCharsets.UTF_8);

		client.connect(config.getHost(), config.getQueryPort());
		client.authPassword(config.getUsername(), config.getPassword());
		session = client.startSession();
		session.startShell();
	} catch (UserAuthException uae) {
		close();
		throw new TS3ConnectionFailedException("Invalid query username or password");
	} catch (IOException ioe) {
		close();
		throw ioe;
	}
}
 
Example 2
Source File: SSHClientRebuilder.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public SSHClient build() throws IOException {
  SSHClient sshClient = new SSHClient(config);
  if (knownHosts != null) {
    sshClient.loadKnownHosts(knownHosts);
  } else {
    sshClient.addHostKeyVerifier(new PromiscuousVerifier());
  }
  KeyProvider keyProvider = null;
  if (password == null) {
    if (keyLocation != null) {
      if (keyPassphrase != null) {
        keyProvider = sshClient.loadKeys(keyLocation, keyPassphrase);
      } else {
        keyProvider = sshClient.loadKeys(keyLocation);
      }
    } else {
      if (keyPassphrase != null) {
        keyProvider = sshClient.loadKeys(keyPlainText, null, PasswordUtils.createOneOff(keyPassphrase.toCharArray()));
      } else {
        keyProvider = sshClient.loadKeys(keyPlainText, null, null);
      }
    }
  }
  sshClient.connect(hostname, port);
  if (password == null) {
    sshClient.authPublickey(username, keyProvider);
  } else {
    sshClient.authPassword(username, password);
  }
  sshClient.setTimeout(socketTimeout * 1000);
  sshClient.setConnectTimeout(connectionTimeout * 1000);
  return sshClient;
}
 
Example 3
Source File: SSHDUnitTest.java    From datacollector with Apache License 2.0 5 votes vote down vote up
protected SSHClient createSSHClient() throws IOException {
  SSHClient sshClient = new SSHClient();
  sshClient.addHostKeyVerifier(new PromiscuousVerifier());
  sshClient.connect("localhost", port);
  Assert.assertTrue(sshClient.isConnected());
  sshClient.authPassword(TESTUSER, TESTPASS);
  Assert.assertTrue(sshClient.isAuthenticated());
  return sshClient;
}
 
Example 4
Source File: Main.java    From aedict with GNU General Public License v3.0 5 votes vote down vote up
private void upload() throws Exception {
    System.out.println("Uploading");
    final SSHClient ssh = new SSHClient();
    ssh.loadKnownHosts();
    String password = config.password;
    if (password == null) {
        System.out.println("Enter password");
        final Scanner s = new Scanner(System.in);
        password = s.nextLine();
        if (MiscUtils.isBlank(password)) {
            throw new RuntimeException("Invalid password: blank");
        }
    }
    System.out.println("Connecting");
    ssh.connect("rt.sk");
    try {
        System.out.println("Authenticating");
        ssh.authPassword("moto", password);
        System.out.println("Uploading version");
        final String targetFName = REMOTE_DIR + "/" + config.getTargetFileName();
        exec(ssh, "echo `date +%Y%m%d` >" + REMOTE_DIR + "/" + config.getTargetFileName() + ".version");
        exec(ssh, "rm -f " + targetFName);
        System.out.println("Uploading");
        final SCPFileTransfer ft = ssh.newSCPFileTransfer();
        ft.upload(config.getTargetFileName(), targetFName);
    } finally {
        ssh.disconnect();
    }
}
 
Example 5
Source File: AwsYcloudHybridCloudTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void testShhAuthenticationFailure(String username, String host) throws IOException {
    SSHClient client = null;
    try {
        client = getSshClient(host);
        client.authPassword(username, MOCK_UMS_PASSWORD_INVALID);
        fail("SSH authentication passed with invalid password.");
    } catch (UserAuthException ex) {
        //Expected
    }
    if (client != null) {
        client.close();
    }
}
 
Example 6
Source File: SftpFileTransferLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
private SSHClient setupSshj() throws IOException {
    SSHClient client = new SSHClient();
    client.addHostKeyVerifier(new PromiscuousVerifier());
    client.connect(remoteHost);
    client.authPassword(username, password);
    return client;
}
 
Example 7
Source File: SshJExample.java    From ExpectIt with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    SSHClient ssh = new SSHClient();
    ssh.addHostKeyVerifier(
            new HostKeyVerifier() {
                @Override
                public boolean verify(String s, int i, PublicKey publicKey) {
                    return true;
                }
            });
    ssh.connect("sdf.org");
    ssh.authPassword("new", "");
    Session session = ssh.startSession();
    session.allocateDefaultPTY();
    Shell shell = session.startShell();
    Expect expect = new ExpectBuilder()
            .withOutput(shell.getOutputStream())
            .withInputs(shell.getInputStream(), shell.getErrorStream())
            .withEchoInput(System.out)
            .withEchoOutput(System.err)
            .withInputFilters(removeColors(), removeNonPrintable())
            .withExceptionOnFailure()
            .build();
    try {
        expect.expect(contains("[RETURN]"));
        expect.sendLine();
        String ipAddress = expect.expect(regexp("Trying (.*)\\.\\.\\.")).group(1);
        System.out.println("Captured IP: " + ipAddress);
        expect.expect(contains("login:"));
        expect.sendLine("new");
        expect.expect(contains("(Y/N)"));
        expect.send("N");
        expect.expect(regexp(": $"));
        expect.send("\b");
        expect.expect(regexp("\\(y\\/n\\)"));
        expect.sendLine("y");
        expect.expect(contains("Would you like to sign the guestbook?"));
        expect.send("n");
        expect.expect(contains("[RETURN]"));
        expect.sendLine();
    } finally {
        expect.close();
        session.close();
        ssh.close();
    }
}
 
Example 8
Source File: AwsYcloudHybridCloudTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
private void testShhAuthenticationSuccessfull(String username, String host) throws IOException, UserAuthException {
    SSHClient client = getSshClient(host);
    client.authPassword(username, MOCK_UMS_PASSWORD);
    client.close();
}