X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.ssh%2Fsrc%2Forg%2Fargeo%2Fcms%2Fssh%2FAbstractSsh.java;h=f2525bff850b0c914709309f4389d0b2679a7671;hb=dbb84b4ec2d313ec0724d035c32f482ac57974c5;hp=2d195de9f3d0f4ce3e9644218d840abc6cdc1d02;hpb=5cbd7544d8243c7f0f0f5c1a78daac979aee20e4;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms.ssh/src/org/argeo/cms/ssh/AbstractSsh.java b/org.argeo.cms.ssh/src/org/argeo/cms/ssh/AbstractSsh.java index 2d195de9f..f2525bff8 100644 --- a/org.argeo.cms.ssh/src/org/argeo/cms/ssh/AbstractSsh.java +++ b/org.argeo.cms.ssh/src/org/argeo/cms/ssh/AbstractSsh.java @@ -20,7 +20,7 @@ import org.apache.sshd.sftp.client.fs.SftpFileSystemProvider; import org.argeo.api.cms.CmsLog; @SuppressWarnings("restriction") -abstract class AbstractSsh { +public abstract class AbstractSsh { private final static CmsLog log = CmsLog.getLog(AbstractSsh.class); private static SshClient sshClient; @@ -31,7 +31,7 @@ abstract class AbstractSsh { private SshKeyPair sshKeyPair; - synchronized SshClient getSshClient() { + public synchronized SshClient getSshClient() { if (sshClient == null) { long begin = System.currentTimeMillis(); sshClient = SshClient.setUpDefaultClient(); @@ -51,33 +51,52 @@ abstract class AbstractSsh { return sftpFileSystemProvider; } - void authenticate() { - try { - if (sshKeyPair != null) { - session.addPublicKeyIdentity(sshKeyPair.asKeyPair()); - } else { - - if (!passwordSet) { - String password; - Console console = System.console(); - if (console == null) {// IDE - System.out.print("Password: "); - try (Scanner s = new Scanner(System.in)) { - password = s.next(); - } - } else { - console.printf("Password: "); - char[] pwd = console.readPassword(); - password = new String(pwd); - Arrays.fill(pwd, ' '); + public void authenticate() { + if (sshKeyPair != null) { + session.addPublicKeyIdentity(sshKeyPair.asKeyPair()); + } else { + + if (!passwordSet) { + String password; + Console console = System.console(); + if (console == null) {// IDE + System.out.print("Password: "); + try (Scanner s = new Scanner(System.in)) { + password = s.next(); } - session.addPasswordIdentity(password); - passwordSet = true; + } else { + console.printf("Password: "); + char[] pwd = console.readPassword(); + password = new String(pwd); + Arrays.fill(pwd, ' '); } + session.addPasswordIdentity(password); + passwordSet = true; } + } + verifyAuth(); + } + + public void verifyAuth() { + try { session.auth().verify(1000l); } catch (IOException e) { - throw new IllegalStateException(e); + throw new IllegalStateException("Cannot verify auth", e); + } + } + + public static char[] readPassword() { + Console console = System.console(); + if (console == null) {// IDE + System.out.print("Password: "); + try (Scanner s = new Scanner(System.in)) { + String password = s.next(); + return password.toCharArray(); + } + } else { + console.printf("Password: "); + char[] pwd = console.readPassword(); + return pwd; } } @@ -136,7 +155,7 @@ abstract class AbstractSsh { } } - void closeSession() { + public void closeSession() { if (session == null) throw new IllegalStateException("No session is open"); try { @@ -156,6 +175,10 @@ abstract class AbstractSsh { this.sshKeyPair = sshKeyPair; } + public static void openShell(AbstractSsh ssh) { + openShell(ssh.getSession()); + } + public static void openShell(ClientSession session) { try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL)) { channel.setIn(new NoCloseInputStream(System.in));