SSH GraalVM configs
[lgpl/argeo-commons.git] / org.argeo.cms.lib.sshd / src / org / argeo / cms / ssh / cli / SshShell.java
index 78903a7791cf0da44b5ecf7672a17c2b7c4cf94f..a7283b3ad1d464b2e33745097c0668834f2ddc22 100644 (file)
@@ -8,6 +8,7 @@ import java.security.KeyPair;
 import java.util.List;
 
 import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.MissingArgumentException;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.apache.sshd.agent.SshAgent;
@@ -17,99 +18,107 @@ import org.apache.sshd.agent.unix.UnixAgentFactory;
 import org.apache.sshd.client.config.keys.ClientIdentityLoader;
 import org.apache.sshd.common.NamedResource;
 import org.apache.sshd.common.config.keys.FilePasswordProvider;
+import org.argeo.api.cli.CommandArgsException;
 import org.argeo.api.cli.DescribedCommand;
 import org.argeo.cms.ssh.AbstractSsh;
 import org.argeo.cms.ssh.Ssh;
 
 public class SshShell implements DescribedCommand<String> {
-               private Option portOption;
-
-               @Override
-               public Options getOptions() {
-                       Options options = new Options();
-                       portOption = Option.builder().option("p").longOpt("port").hasArg().desc("port to connect to").build();
-                       options.addOption(portOption);
-                       return options;
-               }
-
-               @Override
-               public String apply(List<String> args) {
-                       CommandLine cl = toCommandLine(args);
-                       String portStr = cl.getOptionValue(portOption);
-                       if (portStr == null)
-                               portStr = "22";
-
-                       String host = cl.getArgList().get(0);
-
-                       String uriStr = "ssh://" + host + ":" + portStr + "/";
-                       // System.out.println(uriStr);
-                       URI uri = URI.create(uriStr);
-
-                       Ssh ssh = null;
+       private Option portOption;
+
+       @Override
+       public Options getOptions() {
+               Options options = new Options();
+               portOption = Option.builder().option("p").longOpt("port").hasArg().desc("port to connect to").build();
+               options.addOption(portOption);
+               return options;
+       }
+
+       @Override
+       public String apply(List<String> args) {
+               CommandLine cl = toCommandLine(args);
+               String portStr = cl.getOptionValue(portOption);
+               if (portStr == null)
+                       portStr = "22";
+
+               if (cl.getArgList().size() == 0)
+                       throw new CommandArgsException("Host must be provided");
+               String host = cl.getArgList().get(0);
+
+               String uriStr = "ssh://" + host + ":" + portStr + "/";
+               // System.out.println(uriStr);
+               URI uri = URI.create(uriStr);
+
+               Ssh ssh = null;
+               try {
+                       ssh = new Ssh(uri);
+                       boolean osAgent;
+                       SshAgent sshAgent;
                        try {
-                               ssh = new Ssh(uri);
-                               boolean osAgent;
-                               SshAgent sshAgent;
-                               try {
-                                       String sshAuthSockentEnv = System.getenv(SshAgent.SSH_AUTHSOCKET_ENV_NAME);
-                                       if (sshAuthSockentEnv != null) {
-                                               ssh.getSshClient().getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, sshAuthSockentEnv);
-                                               SshAgentFactory agentFactory = new UnixAgentFactory();
-                                               ssh.getSshClient().setAgentFactory(agentFactory);
-                                               sshAgent = agentFactory.createClient(null, ssh.getSshClient());
-                                               osAgent = true;
-                                       } else {
-                                               osAgent = false;
-                                       }
-                               } catch (Exception e) {
-                                       e.printStackTrace();
-                                       osAgent = false;
-                               }
-
-                               if (!osAgent) {
-                                       SshAgentFactory agentFactory = new LocalAgentFactory();
+                               String sshAuthSockentEnv = System.getenv(SshAgent.SSH_AUTHSOCKET_ENV_NAME);
+                               if (sshAuthSockentEnv != null) {
+                                       ssh.getSshClient().getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, sshAuthSockentEnv);
+                                       SshAgentFactory agentFactory = new UnixAgentFactory();
                                        ssh.getSshClient().setAgentFactory(agentFactory);
                                        sshAgent = agentFactory.createClient(null, ssh.getSshClient());
-                                       String keyPath = System.getProperty("user.home") + "/.ssh/id_rsa";
-
-                                       char[] keyPassword = AbstractSsh.readPassword();
-                                       NamedResource namedResource = new NamedResource() {
-
-                                               @Override
-                                               public String getName() {
-                                                       return keyPath;
-                                               }
-                                       };
-                                       KeyPair keyPair = ClientIdentityLoader.DEFAULT
-                                                       .loadClientIdentities(null, namedResource, FilePasswordProvider.of(new String(keyPassword)))
-                                                       .iterator().next();
-                                       sshAgent.addIdentity(keyPair, "NO COMMENT");
+                                       osAgent = true;
+                               } else {
+                                       osAgent = false;
                                }
+                       } catch (Exception e) {
+                               e.printStackTrace();
+                               osAgent = false;
+                       }
+
+                       if (!osAgent) {
+                               SshAgentFactory agentFactory = new LocalAgentFactory();
+                               ssh.getSshClient().setAgentFactory(agentFactory);
+                               sshAgent = agentFactory.createClient(null, ssh.getSshClient());
+                               String keyPath = System.getProperty("user.home") + "/.ssh/id_rsa";
+
+                               char[] keyPassword = AbstractSsh.readPassword();
+                               NamedResource namedResource = new NamedResource() {
+
+                                       @Override
+                                       public String getName() {
+                                               return keyPath;
+                                       }
+                               };
+                               KeyPair keyPair = ClientIdentityLoader.DEFAULT
+                                               .loadClientIdentities(null, namedResource, FilePasswordProvider.of(new String(keyPassword)))
+                                               .iterator().next();
+                               sshAgent.addIdentity(keyPair, "NO COMMENT");
+                       }
 
 //                             char[] keyPassword = AbstractSsh.readPassword();
 //                             SshKeyPair keyPair = SshKeyPair.loadDefault(keyPassword);
 //                             Arrays.fill(keyPassword, '*');
 //                             ssh.setSshKeyPair(keyPair);
 //                             ssh.authenticate();
-                               ssh.verifyAuth();
-
-                               long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
-                               System.out.println("Ssh available in " + jvmUptime + " ms.");
-
-                               AbstractSsh.openShell(ssh);
-                       } catch (IOException | GeneralSecurityException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
-                       } finally {
-                               if (ssh != null)
-                                       ssh.closeSession();
-                       }
-                       return null;
+                       ssh.verifyAuth();
+
+                       long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
+                       System.out.println("Ssh available in " + jvmUptime + " ms.");
+
+                       AbstractSsh.openShell(ssh);
+               } catch (IOException | GeneralSecurityException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } finally {
+                       if (ssh != null)
+                               ssh.closeSession();
                }
+               return null;
+       }
 
-               @Override
-               public String getDescription() {
-                       return "Launch a static CMS.";
-               }
+       @Override
+       public String getUsage() {
+               return "<hostname>";
+       }
+
+       @Override
+       public String getDescription() {
+               return "Opens a remote shell";
+       }
 
-       }
\ No newline at end of file
+}
\ No newline at end of file