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