- System.out.print(keyPath + ": ");
- Scanner s = new Scanner(System.in);
- String password = s.next();
-// KeyPair keyPair = ClientIdentityLoader.DEFAULT.loadClientIdentity(keyPath,
-// FilePasswordProvider.of(password));
-// sshAgent.addIdentity(keyPair, "NO COMMENT");
+
+ String password;
+ Console console = System.console();
+ if (console != null) {
+ password = new String(console.readPassword(keyPath + ": "));
+ } else {
+ System.out.print(keyPath + ": ");
+ try (Scanner s = new Scanner(System.in)) {
+ password = s.next();
+ }
+ }
+ NamedResource namedResource = new NamedResource() {
+
+ @Override
+ public String getName() {
+ return keyPath;
+ }
+ };
+ KeyPair keyPair = ClientIdentityLoader.DEFAULT
+ .loadClientIdentities(null, namedResource, FilePasswordProvider.of(password)).iterator().next();
+ sshAgent.addIdentity(keyPair, "NO COMMENT");