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