X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.core%2Fsrc%2Forg%2Fargeo%2Fssh%2FSsh.java;h=68dd912ec1ff0753d38abf963294e6651c1ac01a;hb=5412d591384a4e0a6508a468f39d9349b40a8c01;hp=584294cc07228c91052597ecfec9c36ca7e334b5;hpb=5c6333d04de4985c349197852414faa0f4ee33ee;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.core/src/org/argeo/ssh/Ssh.java b/org.argeo.core/src/org/argeo/ssh/Ssh.java index 584294cc0..68dd912ec 100644 --- a/org.argeo.core/src/org/argeo/ssh/Ssh.java +++ b/org.argeo.core/src/org/argeo/ssh/Ssh.java @@ -1,11 +1,8 @@ package org.argeo.ssh; -import java.io.IOException; import java.net.URI; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; @@ -13,38 +10,20 @@ import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; -import org.apache.sshd.client.channel.ClientChannel; -import org.apache.sshd.client.channel.ClientChannelEvent; -import org.apache.sshd.client.session.ClientSession; -import org.apache.sshd.common.util.io.NoCloseInputStream; -import org.apache.sshd.common.util.io.NoCloseOutputStream; +/** Create an SSH shell. */ public class Ssh extends AbstractSsh { private final URI uri; + public Ssh(String username, String host, int port) { + this(AbstractSsh.toUri(username, host, port)); + } + public Ssh(URI uri) { this.uri = uri; openSession(uri); } - static void openShell(ClientSession session) { - try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL)) { - channel.setIn(new NoCloseInputStream(System.in)); - channel.setOut(new NoCloseOutputStream(System.out)); - channel.setErr(new NoCloseOutputStream(System.err)); - channel.open(); - - Set events = new HashSet<>(); - events.add(ClientChannelEvent.CLOSED); - channel.waitFor(events, 0); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } finally { - session.close(false); - } - } - public static void main(String[] args) { Options options = getOptions(); CommandLineParser parser = new DefaultParser(); @@ -69,7 +48,7 @@ public class Ssh extends AbstractSsh { ssh.authenticate(); if (command.size() == 0) {// shell - openShell(ssh.getSession()); + AbstractSsh.openShell(ssh.getSession()); } else {// execute command } @@ -83,6 +62,10 @@ public class Ssh extends AbstractSsh { } } + public URI getUri() { + return uri; + } + public static Options getOptions() { Options options = new Options(); // options.addOption("p", true, "port");