import org.apache.sshd.client.channel.ClientChannelEvent;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.session.ClientSession;
-import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystemProvider;
-import org.apache.sshd.common.util.io.NoCloseInputStream;
-import org.apache.sshd.common.util.io.NoCloseOutputStream;
+import org.apache.sshd.common.util.io.input.NoCloseInputStream;
+import org.apache.sshd.common.util.io.output.NoCloseOutputStream;
+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;
private SshKeyPair sshKeyPair;
- synchronized SshClient getSshClient() {
+ public synchronized SshClient getSshClient() {
if (sshClient == null) {
long begin = System.currentTimeMillis();
sshClient = SshClient.setUpDefaultClient();
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;
}
}
}
}
- void closeSession() {
+ public void closeSession() {
if (session == null)
throw new IllegalStateException("No session is open");
try {
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));