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
;
10 import java
.util
.Scanner
;
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
;
24 public class SshSync
{
25 private final static CmsLog log
= CmsLog
.getLog(SshSync
.class);
27 public static void main(String
[] args
) {
29 try (SshClient client
= SshClient
.setUpDefaultClient()) {
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
);
37 String login
= System
.getProperty("user.name");
38 String host
= "localhost";
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");
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());
56 ConnectFuture connectFuture
= client
.connect(login
, host
, port
);
57 connectFuture
.await();
58 ClientSession session
= connectFuture
.getSession();
62 // session.addPasswordIdentity(new String(password));
63 session
.auth().verify(1000l);
65 SftpFileSystemProvider fsProvider
= new SftpFileSystemProvider(client
);
67 SftpFileSystem fs
= fsProvider
.newFileSystem(session
);
68 Path testPath
= fs
.getPath("/home/" + login
+ "/tmp");
69 Files
.list(testPath
).forEach(System
.out
::println
);
75 } catch (Exception e
) {
76 // TODO Auto-generated catch block
81 static void test(Path testBase
) {
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
);
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
);
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
);
117 Path listedFile
= null;
118 for (Path file
: files
) {
120 if (!Files
.isDirectory(file
))
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