]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.core/src/org/argeo/ssh/Ssh.java
3 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
6 import java
.util
.HashSet
;
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
;
22 public class Ssh
extends AbstractSsh
{
23 private final URI uri
;
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
));
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
48 public static void main(String
[] args
) {
49 Options options
= getOptions();
50 CommandLineParser parser
= new DefaultParser();
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");
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
));
68 Ssh ssh
= new Ssh(uri
);
71 if (command
.size() == 0) {// shell
72 openShell(ssh
.getSession());
73 } else {// execute command
77 } catch (Exception exp
) {
78 exp
.printStackTrace();
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());
94 public static void printHelp(Options options
) {
95 HelpFormatter formatter
= new HelpFormatter();
96 formatter
.printHelp("ssh [username@]hostname", options
, true);