Allow to pass SSH password via an InputStream
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 7 Mar 2024 16:39:59 +0000 (17:39 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 7 Mar 2024 16:39:59 +0000 (17:39 +0100)
org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/AbstractSsh.java

index c91ab48058f06944489e559a519b6b0789eb348a..8d9e42e2903066ae2dfb43c95c4368238ada331b 100644 (file)
@@ -2,6 +2,7 @@ package org.argeo.cms.ssh;
 
 import java.io.Console;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Arrays;
@@ -51,16 +52,20 @@ public abstract class AbstractSsh {
        }
 
        public void authenticate() {
+               authenticate(System.in);
+       }
+
+       public void authenticate(InputStream in) {
                if (sshKeyPair != null) {
                        session.addPublicKeyIdentity(sshKeyPair.asKeyPair());
                } else {
 
-                       if (!passwordSet) {
+                       if (!passwordSet && in != null) {
                                String password;
                                Console console = System.console();
                                if (console == null) {// IDE
                                        System.out.print("Password: ");
-                                       try (Scanner s = new Scanner(System.in)) {
+                                       try (Scanner s = new Scanner(in)) {
                                                password = s.next();
                                        }
                                } else {