]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.maintenance/src/org/argeo/maintenance/backup/SshSync.java
First tests with Apache SSHD client
[lgpl/argeo-commons.git] / org.argeo.maintenance / src / org / argeo / maintenance / backup / SshSync.java
1 package org.argeo.maintenance.backup;
2
3 import java.io.Console;
4 import java.io.IOException;
5 import java.util.HashSet;
6 import java.util.Set;
7
8 import org.apache.sshd.client.SshClient;
9 import org.apache.sshd.client.channel.ClientChannel;
10 import org.apache.sshd.client.channel.ClientChannelEvent;
11 import org.apache.sshd.client.future.ConnectFuture;
12 import org.apache.sshd.client.session.ClientSession;
13 import org.apache.sshd.common.util.io.NoCloseInputStream;
14 import org.apache.sshd.common.util.io.NoCloseOutputStream;
15
16 public class SshSync {
17 public static void main(String[] args) {
18
19 String login = System.getProperty("user.name");
20 Console console = System.console();
21 char[] password = console.readPassword();
22 String host = "localhost";
23 int port = 22;
24
25 try (SshClient client = SshClient.setUpDefaultClient()) {
26 client.start();
27
28 // SimpleClient simpleClient= AbstractSimpleClientSessionCreator.wrap(client, null);
29 // simpleClient.sessionLogin(host, login, password);
30
31 ConnectFuture connectFuture = client.connect(login, host, port);
32 connectFuture.await();
33 ClientSession session = connectFuture.getSession();
34
35 try {
36
37 session.addPasswordIdentity(new String(password));
38 session.auth().verify(1000l);
39
40 try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL)) {
41 channel.setIn(new NoCloseInputStream(System.in));
42 channel.setOut(new NoCloseOutputStream(System.out));
43 channel.setErr(new NoCloseOutputStream(System.err));
44 channel.open();
45
46 Set<ClientChannelEvent> events = new HashSet<>();
47 events.add(ClientChannelEvent.CLOSED);
48 channel.waitFor(events, 0);
49 } finally {
50 session.close(false);
51 }
52 } finally {
53 client.stop();
54 }
55 } catch (IOException e) {
56 // TODO Auto-generated catch block
57 e.printStackTrace();
58 }
59 }
60 }