]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/Ssh.java
1 package org
.argeo
.cms
.ssh
;
4 import java
.util
.ArrayList
;
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
;
14 /** Create an SSH shell. */
15 public class Ssh
extends AbstractSsh
{
16 private final URI uri
;
18 public Ssh(String username
, String host
, int port
) {
19 this(AbstractSsh
.toUri(username
, host
, port
));
27 public static void main(String
[] args
) {
28 Options options
= getOptions();
29 CommandLineParser parser
= new DefaultParser();
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");
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
));
47 Ssh ssh
= new Ssh(uri
);
50 if (command
.size() == 0) {// shell
51 AbstractSsh
.openShell(ssh
.getSession());
52 } else {// execute command
56 } catch (Exception exp
) {
57 exp
.printStackTrace();
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());
77 public static void printHelp(Options options
) {
78 HelpFormatter formatter
= new HelpFormatter();
79 formatter
.printHelp("ssh [username@]hostname", options
, true);