1 package org
.argeo
.cms
.ssh
.cli
;
3 import java
.io
.IOException
;
4 import java
.lang
.management
.ManagementFactory
;
6 import java
.security
.GeneralSecurityException
;
7 import java
.security
.KeyPair
;
10 import org
.apache
.commons
.cli
.CommandLine
;
11 import org
.apache
.commons
.cli
.Option
;
12 import org
.apache
.commons
.cli
.Options
;
13 import org
.apache
.sshd
.agent
.SshAgent
;
14 import org
.apache
.sshd
.agent
.SshAgentFactory
;
15 import org
.apache
.sshd
.agent
.local
.LocalAgentFactory
;
16 import org
.apache
.sshd
.agent
.unix
.UnixAgentFactory
;
17 import org
.apache
.sshd
.client
.config
.keys
.ClientIdentityLoader
;
18 import org
.apache
.sshd
.common
.NamedResource
;
19 import org
.apache
.sshd
.common
.config
.keys
.FilePasswordProvider
;
20 import org
.argeo
.api
.cli
.DescribedCommand
;
21 import org
.argeo
.cms
.ssh
.AbstractSsh
;
22 import org
.argeo
.cms
.ssh
.Ssh
;
24 public class SshShell
implements DescribedCommand
<String
> {
25 private Option portOption
;
28 public Options
getOptions() {
29 Options options
= new Options();
30 portOption
= Option
.builder().option("p").longOpt("port").hasArg().desc("port to connect to").build();
31 options
.addOption(portOption
);
36 public String
apply(List
<String
> args
) {
37 CommandLine cl
= toCommandLine(args
);
38 String portStr
= cl
.getOptionValue(portOption
);
42 String host
= cl
.getArgList().get(0);
44 String uriStr
= "ssh://" + host
+ ":" + portStr
+ "/";
45 // System.out.println(uriStr);
46 URI uri
= URI
.create(uriStr
);
54 String sshAuthSockentEnv
= System
.getenv(SshAgent
.SSH_AUTHSOCKET_ENV_NAME
);
55 if (sshAuthSockentEnv
!= null) {
56 ssh
.getSshClient().getProperties().put(SshAgent
.SSH_AUTHSOCKET_ENV_NAME
, sshAuthSockentEnv
);
57 SshAgentFactory agentFactory
= new UnixAgentFactory();
58 ssh
.getSshClient().setAgentFactory(agentFactory
);
59 sshAgent
= agentFactory
.createClient(null, ssh
.getSshClient());
64 } catch (Exception e
) {
70 SshAgentFactory agentFactory
= new LocalAgentFactory();
71 ssh
.getSshClient().setAgentFactory(agentFactory
);
72 sshAgent
= agentFactory
.createClient(null, ssh
.getSshClient());
73 String keyPath
= System
.getProperty("user.home") + "/.ssh/id_rsa";
75 char[] keyPassword
= AbstractSsh
.readPassword();
76 NamedResource namedResource
= new NamedResource() {
79 public String
getName() {
83 KeyPair keyPair
= ClientIdentityLoader
.DEFAULT
84 .loadClientIdentities(null, namedResource
, FilePasswordProvider
.of(new String(keyPassword
)))
86 sshAgent
.addIdentity(keyPair
, "NO COMMENT");
89 // char[] keyPassword = AbstractSsh.readPassword();
90 // SshKeyPair keyPair = SshKeyPair.loadDefault(keyPassword);
91 // Arrays.fill(keyPassword, '*');
92 // ssh.setSshKeyPair(keyPair);
93 // ssh.authenticate();
96 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
97 System
.out
.println("Ssh available in " + jvmUptime
+ " ms.");
99 AbstractSsh
.openShell(ssh
);
100 } catch (IOException
| GeneralSecurityException e
) {
101 // TODO Auto-generated catch block
111 public String
getDescription() {
112 return "Launch a static CMS.";