1 package org
.argeo
.cms
.ssh
;
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
;
12 import java
.util
.Scanner
;
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
;
29 public class SshSync
{
30 private final static CmsLog log
= CmsLog
.getLog(SshSync
.class);
32 public static void main(String
[] args
) {
34 try (SshClient client
= SshClient
.setUpDefaultClient()) {
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
);
42 String login
= System
.getProperty("user.name");
43 String host
= "localhost";
47 String keyPath
= "/home/" + login
+ "/.ssh/id_rsa";
50 Console console
= System
.console();
51 if (console
!= null) {
52 password
= new String(console
.readPassword(keyPath
+ ": "));
54 System
.out
.print(keyPath
+ ": ");
55 try (Scanner s
= new Scanner(System
.in
)) {
59 NamedResource namedResource
= new NamedResource() {
62 public String
getName() {
66 KeyPair keyPair
= ClientIdentityLoader
.DEFAULT
67 .loadClientIdentities(null, namedResource
, FilePasswordProvider
.of(password
)).iterator().next();
68 sshAgent
.addIdentity(keyPair
, "NO COMMENT");
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());
76 ConnectFuture connectFuture
= client
.connect(login
, host
, port
);
77 connectFuture
.await();
78 ClientSession session
= connectFuture
.getSession();
82 // session.addPasswordIdentity(new String(password));
83 session
.auth().verify(1000l);
85 SftpFileSystemProvider fsProvider
= new SftpFileSystemProvider(client
);
87 SftpFileSystem fs
= fsProvider
.newFileSystem(session
);
88 Path testPath
= fs
.getPath("/home/" + login
+ "/tmp");
89 Files
.list(testPath
).forEach(System
.out
::println
);
95 } catch (Exception e
) {
96 // TODO Auto-generated catch block
101 static void test(Path testBase
) {
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
);
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
);
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
);
137 Path listedFile
= null;
138 for (Path file
: files
) {
140 if (!Files
.isDirectory(file
))
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