]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/Ssh.java
Workaround Krb5LoginModule printing to System.out when tryFirstPass is
[lgpl/argeo-commons.git] / org.argeo.cms.lib.sshd / src / org / argeo / cms / ssh / Ssh.java
1 package org.argeo.cms.ssh;
2
3 import java.net.URI;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.apache.commons.cli.CommandLine;
8 import org.apache.commons.cli.CommandLineParser;
9 import org.apache.commons.cli.DefaultParser;
10 import org.apache.commons.cli.HelpFormatter;
11 import org.apache.commons.cli.Option;
12 import org.apache.commons.cli.Options;
13
14 /** Create an SSH shell. */
15 public class Ssh extends AbstractSsh {
16 private final URI uri;
17
18 public Ssh(String username, String host, int port) {
19 this(AbstractSsh.toUri(username, host, port));
20 }
21
22 public Ssh(URI uri) {
23 this.uri = uri;
24 openSession(uri);
25 }
26
27 public static void main(String[] args) {
28 Options options = getOptions();
29 CommandLineParser parser = new DefaultParser();
30 try {
31 CommandLine line = parser.parse(options, args);
32 List<String> remaining = line.getArgList();
33 if (remaining.size() == 0) {
34 System.err.println("There must be at least one argument");
35 printHelp(options);
36 System.exit(1);
37 }
38 URI uri = new URI("ssh://" + remaining.get(0));
39 List<String> command = new ArrayList<>();
40 if (remaining.size() > 1) {
41 for (int i = 1; i < remaining.size(); i++) {
42 command.add(remaining.get(i));
43 }
44 }
45
46 // auth
47 Ssh ssh = new Ssh(uri);
48 ssh.authenticate();
49
50 if (command.size() == 0) {// shell
51 AbstractSsh.openShell(ssh.getSession());
52 } else {// execute command
53
54 }
55 ssh.closeSession();
56 } catch (Exception exp) {
57 exp.printStackTrace();
58 printHelp(options);
59 System.exit(1);
60 } finally {
61
62 }
63 }
64
65 public URI getUri() {
66 return uri;
67 }
68
69 public static Options getOptions() {
70 Options options = new Options();
71 // options.addOption("p", true, "port");
72 options.addOption(Option.builder("p").hasArg().argName("port").desc("port of the SSH server").build());
73
74 return options;
75 }
76
77 public static void printHelp(Options options) {
78 HelpFormatter formatter = new HelpFormatter();
79 formatter.printHelp("ssh [username@]hostname", options, true);
80 }
81 }