]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.core/src/org/argeo/ssh/Ssh.java
Make web socket configuration more extensible.
[lgpl/argeo-commons.git] / org.argeo.core / src / org / argeo / ssh / Ssh.java
1 package org.argeo.ssh;
2
3 import java.io.IOException;
4 import java.net.URI;
5 import java.util.ArrayList;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Set;
9
10 import org.apache.commons.cli.CommandLine;
11 import org.apache.commons.cli.CommandLineParser;
12 import org.apache.commons.cli.DefaultParser;
13 import org.apache.commons.cli.HelpFormatter;
14 import org.apache.commons.cli.Option;
15 import org.apache.commons.cli.Options;
16 import org.apache.sshd.client.channel.ClientChannel;
17 import org.apache.sshd.client.channel.ClientChannelEvent;
18 import org.apache.sshd.client.session.ClientSession;
19 import org.apache.sshd.common.util.io.NoCloseInputStream;
20 import org.apache.sshd.common.util.io.NoCloseOutputStream;
21
22 public class Ssh extends AbstractSsh {
23 private final URI uri;
24
25 public Ssh(URI uri) {
26 this.uri = uri;
27 openSession(uri);
28 }
29
30 static void openShell(ClientSession session) {
31 try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL)) {
32 channel.setIn(new NoCloseInputStream(System.in));
33 channel.setOut(new NoCloseOutputStream(System.out));
34 channel.setErr(new NoCloseOutputStream(System.err));
35 channel.open();
36
37 Set<ClientChannelEvent> events = new HashSet<>();
38 events.add(ClientChannelEvent.CLOSED);
39 channel.waitFor(events, 0);
40 } catch (IOException e) {
41 // TODO Auto-generated catch block
42 e.printStackTrace();
43 } finally {
44 session.close(false);
45 }
46 }
47
48 public static void main(String[] args) {
49 Options options = getOptions();
50 CommandLineParser parser = new DefaultParser();
51 try {
52 CommandLine line = parser.parse(options, args);
53 List<String> remaining = line.getArgList();
54 if (remaining.size() == 0) {
55 System.err.println("There must be at least one argument");
56 printHelp(options);
57 System.exit(1);
58 }
59 URI uri = new URI("ssh://" + remaining.get(0));
60 List<String> command = new ArrayList<>();
61 if (remaining.size() > 1) {
62 for (int i = 1; i < remaining.size(); i++) {
63 command.add(remaining.get(i));
64 }
65 }
66
67 // auth
68 Ssh ssh = new Ssh(uri);
69 ssh.authenticate();
70
71 if (command.size() == 0) {// shell
72 openShell(ssh.getSession());
73 } else {// execute command
74
75 }
76 ssh.closeSession();
77 } catch (Exception exp) {
78 exp.printStackTrace();
79 printHelp(options);
80 System.exit(1);
81 } finally {
82
83 }
84 }
85
86 public static Options getOptions() {
87 Options options = new Options();
88 // options.addOption("p", true, "port");
89 options.addOption(Option.builder("p").hasArg().argName("port").desc("port of the SSH server").build());
90
91 return options;
92 }
93
94 public static void printHelp(Options options) {
95 HelpFormatter formatter = new HelpFormatter();
96 formatter.printHelp("ssh [username@]hostname", options, true);
97 }
98 }