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