1 package org
.argeo
.sync
.fs
;
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
.security
.KeyPair
;
10 import java
.security
.PublicKey
;
11 import java
.util
.HashSet
;
12 import java
.util
.List
;
14 import java
.util
.Scanner
;
17 import org
.apache
.commons
.io
.IOUtils
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.apache
.commons
.logging
.LogFactory
;
20 import org
.apache
.sshd
.agent
.SshAgent
;
21 import org
.apache
.sshd
.agent
.SshAgentFactory
;
22 import org
.apache
.sshd
.agent
.local
.LocalAgentFactory
;
23 import org
.apache
.sshd
.agent
.unix
.UnixAgentFactory
;
24 import org
.apache
.sshd
.client
.SshClient
;
25 import org
.apache
.sshd
.client
.channel
.ClientChannel
;
26 import org
.apache
.sshd
.client
.channel
.ClientChannelEvent
;
27 import org
.apache
.sshd
.client
.config
.keys
.ClientIdentityLoader
;
28 import org
.apache
.sshd
.client
.future
.ConnectFuture
;
29 import org
.apache
.sshd
.client
.session
.ClientSession
;
30 import org
.apache
.sshd
.client
.subsystem
.sftp
.SftpFileSystem
;
31 import org
.apache
.sshd
.client
.subsystem
.sftp
.SftpFileSystemProvider
;
32 import org
.apache
.sshd
.common
.config
.keys
.FilePasswordProvider
;
33 import org
.apache
.sshd
.common
.util
.io
.NoCloseInputStream
;
34 import org
.apache
.sshd
.common
.util
.io
.NoCloseOutputStream
;
36 public class SshSync
{
37 private final static Log log
= LogFactory
.getLog(SshSync
.class);
39 public static void main(String
[] args
) {
41 try (SshClient client
= SshClient
.setUpDefaultClient()) {
43 boolean osAgent
= false;
44 SshAgentFactory agentFactory
= osAgent ?
new UnixAgentFactory() : new LocalAgentFactory();
45 // SshAgentFactory agentFactory = new LocalAgentFactory();
46 client
.setAgentFactory(agentFactory
);
47 SshAgent sshAgent
= agentFactory
.createClient(client
);
49 String login
= System
.getProperty("user.name");
50 String host
= "localhost";
54 String keyPath
= "/home/" + login
+ "/.ssh/id_rsa";
55 System
.out
.print(keyPath
+ ": ");
56 Scanner s
= new Scanner(System
.in
);
57 String password
= s
.next();
58 KeyPair keyPair
= ClientIdentityLoader
.DEFAULT
.loadClientIdentity(keyPath
,
59 FilePasswordProvider
.of(password
));
60 sshAgent
.addIdentity(keyPair
, "NO COMMENT");
63 List
<?
extends Map
.Entry
<PublicKey
, String
>> identities
= sshAgent
.getIdentities();
64 for (Map
.Entry
<PublicKey
, String
> entry
: identities
) {
65 System
.out
.println(entry
.getValue() + " : " + entry
.getKey());
68 ConnectFuture connectFuture
= client
.connect(login
, host
, port
);
69 connectFuture
.await();
70 ClientSession session
= connectFuture
.getSession();
74 // session.addPasswordIdentity(new String(password));
75 session
.auth().verify(1000l);
77 SftpFileSystemProvider fsProvider
= new SftpFileSystemProvider(client
);
79 SftpFileSystem fs
= fsProvider
.newFileSystem(session
);
80 Path testPath
= fs
.getPath("/home/" + login
+ "/tmp");
81 Files
.list(testPath
).forEach(System
.out
::println
);
87 } catch (Exception e
) {
88 // TODO Auto-generated catch block
93 static void test(Path testBase
) {
95 Path testPath
= testBase
.resolve("ssh-test.txt");
96 Files
.createFile(testPath
);
97 log
.debug("Created file " + testPath
);
98 Files
.delete(testPath
);
99 log
.debug("Deleted " + testPath
);
100 String txt
= "TEST\nTEST2\n";
101 byte[] arr
= txt
.getBytes();
102 Files
.write(testPath
, arr
);
103 log
.debug("Wrote " + testPath
);
104 byte[] read
= Files
.readAllBytes(testPath
);
105 log
.debug("Read " + testPath
);
106 Path testDir
= testBase
.resolve("testDir");
107 log
.debug("Resolved " + testDir
);
109 Files
.createDirectory(testDir
);
110 log
.debug("Created directory " + testDir
);
111 Path subsubdir
= Files
.createDirectories(testDir
.resolve("subdir/subsubdir"));
112 log
.debug("Created sub directories " + subsubdir
);
113 Path copiedFile
= testDir
.resolve("copiedFile.txt");
114 log
.debug("Resolved " + copiedFile
);
115 Path relativeCopiedFile
= testDir
.relativize(copiedFile
);
116 log
.debug("Relative copied file " + relativeCopiedFile
);
117 try (OutputStream out
= Files
.newOutputStream(copiedFile
);
118 InputStream in
= Files
.newInputStream(testPath
)) {
119 IOUtils
.copy(in
, out
);
121 log
.debug("Copied " + testPath
+ " to " + copiedFile
);
122 Files
.delete(testPath
);
123 log
.debug("Deleted " + testPath
);
124 byte[] copiedRead
= Files
.readAllBytes(copiedFile
);
125 log
.debug("Read " + copiedFile
);
126 // Browse directories
127 DirectoryStream
<Path
> files
= Files
.newDirectoryStream(testDir
);
129 Path listedFile
= null;
130 for (Path file
: files
) {
132 if (!Files
.isDirectory(file
))
135 log
.debug("Listed " + testDir
);
136 // Generic attributes
137 Map
<String
, Object
> attrs
= Files
.readAttributes(copiedFile
, "*");
138 log
.debug("Read attributes of " + copiedFile
+ ": " + attrs
.keySet());
139 } catch (IOException e
) {
140 // TODO Auto-generated catch block
146 static void openShell(ClientSession session
) {
147 try (ClientChannel channel
= session
.createChannel(ClientChannel
.CHANNEL_SHELL
)) {
148 channel
.setIn(new NoCloseInputStream(System
.in
));
149 channel
.setOut(new NoCloseOutputStream(System
.out
));
150 channel
.setErr(new NoCloseOutputStream(System
.err
));
153 Set
<ClientChannelEvent
> events
= new HashSet
<>();
154 events
.add(ClientChannelEvent
.CLOSED
);
155 channel
.waitFor(events
, 0);
156 } catch (IOException e
) {
157 // TODO Auto-generated catch block
160 session
.close(false);