]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.core/src/org/argeo/sync/fs/SshSync.java
Start using FreeMarker
[lgpl/argeo-commons.git] / org.argeo.core / src / org / argeo / sync / fs / SshSync.java
1 package org.argeo.sync.fs;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.nio.file.DirectoryStream;
7 import java.nio.file.Files;
8 import java.nio.file.Path;
9 import java.security.KeyPair;
10 import java.security.PublicKey;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Scanner;
15 import java.util.Set;
16
17 import org.apache.commons.io.IOUtils;
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.sshd.agent.SshAgent;
21 import org.apache.sshd.agent.SshAgentFactory;
22 import org.apache.sshd.agent.local.LocalAgentFactory;
23 import org.apache.sshd.agent.unix.UnixAgentFactory;
24 import org.apache.sshd.client.SshClient;
25 import org.apache.sshd.client.channel.ClientChannel;
26 import org.apache.sshd.client.channel.ClientChannelEvent;
27 import org.apache.sshd.client.config.keys.ClientIdentityLoader;
28 import org.apache.sshd.client.future.ConnectFuture;
29 import org.apache.sshd.client.session.ClientSession;
30 import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystem;
31 import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystemProvider;
32 import org.apache.sshd.common.config.keys.FilePasswordProvider;
33 import org.apache.sshd.common.util.io.NoCloseInputStream;
34 import org.apache.sshd.common.util.io.NoCloseOutputStream;
35
36 public class SshSync {
37 private final static Log log = LogFactory.getLog(SshSync.class);
38
39 public static void main(String[] args) {
40
41 try (SshClient client = SshClient.setUpDefaultClient()) {
42 client.start();
43 boolean osAgent = true;
44 SshAgentFactory agentFactory = osAgent ? new UnixAgentFactory() : new LocalAgentFactory();
45 // SshAgentFactory agentFactory = new LocalAgentFactory();
46 client.setAgentFactory(agentFactory);
47 SshAgent sshAgent = agentFactory.createClient(client);
48
49 String login = System.getProperty("user.name");
50 String host = "localhost";
51 int port = 22;
52
53 if (!osAgent) {
54 String keyPath = "/home/" + login + "/.ssh/id_rsa";
55 System.out.print(keyPath + ": ");
56 Scanner s = new Scanner(System.in);
57 String password = s.next();
58 // KeyPair keyPair = ClientIdentityLoader.DEFAULT.loadClientIdentity(keyPath,
59 // FilePasswordProvider.of(password));
60 // sshAgent.addIdentity(keyPair, "NO COMMENT");
61 }
62
63 // List<? extends Map.Entry<PublicKey, String>> identities = sshAgent.getIdentities();
64 // for (Map.Entry<PublicKey, String> entry : identities) {
65 // System.out.println(entry.getValue() + " : " + entry.getKey());
66 // }
67
68 ConnectFuture connectFuture = client.connect(login, host, port);
69 connectFuture.await();
70 ClientSession session = connectFuture.getSession();
71
72 try {
73
74 // session.addPasswordIdentity(new String(password));
75 session.auth().verify(1000l);
76
77 SftpFileSystemProvider fsProvider = new SftpFileSystemProvider(client);
78
79 SftpFileSystem fs = fsProvider.newFileSystem(session);
80 Path testPath = fs.getPath("/home/" + login + "/tmp");
81 Files.list(testPath).forEach(System.out::println);
82 test(testPath);
83
84 } finally {
85 client.stop();
86 }
87 } catch (Exception e) {
88 // TODO Auto-generated catch block
89 e.printStackTrace();
90 }
91 }
92
93 static void test(Path testBase) {
94 try {
95 Path testPath = testBase.resolve("ssh-test.txt");
96 Files.createFile(testPath);
97 log.debug("Created file " + testPath);
98 Files.delete(testPath);
99 log.debug("Deleted " + testPath);
100 String txt = "TEST\nTEST2\n";
101 byte[] arr = txt.getBytes();
102 Files.write(testPath, arr);
103 log.debug("Wrote " + testPath);
104 byte[] read = Files.readAllBytes(testPath);
105 log.debug("Read " + testPath);
106 Path testDir = testBase.resolve("testDir");
107 log.debug("Resolved " + testDir);
108 // Copy
109 Files.createDirectory(testDir);
110 log.debug("Created directory " + testDir);
111 Path subsubdir = Files.createDirectories(testDir.resolve("subdir/subsubdir"));
112 log.debug("Created sub directories " + subsubdir);
113 Path copiedFile = testDir.resolve("copiedFile.txt");
114 log.debug("Resolved " + copiedFile);
115 Path relativeCopiedFile = testDir.relativize(copiedFile);
116 log.debug("Relative copied file " + relativeCopiedFile);
117 try (OutputStream out = Files.newOutputStream(copiedFile);
118 InputStream in = Files.newInputStream(testPath)) {
119 IOUtils.copy(in, out);
120 }
121 log.debug("Copied " + testPath + " to " + copiedFile);
122 Files.delete(testPath);
123 log.debug("Deleted " + testPath);
124 byte[] copiedRead = Files.readAllBytes(copiedFile);
125 log.debug("Read " + copiedFile);
126 // Browse directories
127 DirectoryStream<Path> files = Files.newDirectoryStream(testDir);
128 int fileCount = 0;
129 Path listedFile = null;
130 for (Path file : files) {
131 fileCount++;
132 if (!Files.isDirectory(file))
133 listedFile = file;
134 }
135 log.debug("Listed " + testDir);
136 // Generic attributes
137 Map<String, Object> attrs = Files.readAttributes(copiedFile, "*");
138 log.debug("Read attributes of " + copiedFile + ": " + attrs.keySet());
139 } catch (IOException e) {
140 // TODO Auto-generated catch block
141 e.printStackTrace();
142 }
143
144 }
145
146 }