String hostname = reader.readLine();
// Make a client connection
SshClient ssh = new SshClient();
// Connect to the host
ssh.connect(hostname);
PublicKeyAuthenticationClient pk = new PublicKeyAuthenticationClient();
// Get the users name
System.out.print("Username? ");
String username = reader.readLine();
pk.setUsername(username);
// Get the private key file
System.out.print("Path to private key file? ");
String filename = reader.readLine();
// Open up the private key file
SshPrivateKeyFile file =
SshPrivateKeyFile.parse(new File(filename));
// If the private key is passphrase protected then ask for the passphrase
String passphrase = null;
if (file.isPassphraseProtected()) {
System.out.print("Enter passphrase? ");
passphrase = reader.readLine();
}
// Get the key
SshPrivateKey key = file.toPrivateKey(passphrase);
pk.setKey(key);
// Try the authentication
int result = ssh.authenticate(pk);
// Evaluate the result
if (result == AuthenticationProtocolState.COMPLETE) {
// The connection is authenticated we can now do some real work!