3 import java
.io
.Console
;
4 import java
.io
.IOException
;
6 import java
.security
.GeneralSecurityException
;
7 import java
.security
.KeyPair
;
8 import java
.util
.Arrays
;
9 import java
.util
.Scanner
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.apache
.sshd
.client
.SshClient
;
14 import org
.apache
.sshd
.client
.config
.keys
.ClientIdentityLoader
;
15 import org
.apache
.sshd
.client
.future
.ConnectFuture
;
16 import org
.apache
.sshd
.client
.session
.ClientSession
;
17 import org
.apache
.sshd
.client
.subsystem
.sftp
.SftpFileSystemProvider
;
18 import org
.apache
.sshd
.common
.config
.keys
.FilePasswordProvider
;
20 abstract class AbstractSsh
{
21 private final static Log log
= LogFactory
.getLog(AbstractSsh
.class);
23 private static SshClient sshClient
;
24 private static SftpFileSystemProvider sftpFileSystemProvider
;
26 private boolean passwordSet
= false;
27 private ClientSession session
;
29 synchronized SshClient
getSshClient() {
30 if (sshClient
== null) {
31 long begin
= System
.currentTimeMillis();
32 sshClient
= SshClient
.setUpDefaultClient();
34 long duration
= System
.currentTimeMillis() - begin
;
35 if (log
.isDebugEnabled())
36 log
.debug("SSH client started in " + duration
+ " ms");
37 Runtime
.getRuntime().addShutdownHook(new Thread(() -> sshClient
.stop(), "Stop SSH client"));
42 synchronized SftpFileSystemProvider
getSftpFileSystemProvider() {
43 if (sftpFileSystemProvider
== null) {
44 sftpFileSystemProvider
= new SftpFileSystemProvider(sshClient
);
46 return sftpFileSystemProvider
;
53 Console console
= System
.console();
54 if (console
== null) {// IDE
55 System
.out
.print("Password: ");
56 Scanner s
= new Scanner(System
.in
);
59 console
.printf("Password: ");
60 char[] pwd
= console
.readPassword();
61 password
= new String(pwd
);
62 Arrays
.fill(pwd
, ' ');
64 session
.addPasswordIdentity(password
);
67 session
.auth().verify(1000l);
68 } catch (IOException e
) {
69 throw new IllegalStateException(e
);
73 void addPassword(String password
) {
74 session
.addPasswordIdentity(password
);
77 void loadKey(String password
) {
78 loadKey(password
, System
.getProperty("user.home") + "/.ssh/id_rsa");
81 void loadKey(String password
, String keyPath
) {
83 KeyPair keyPair
= ClientIdentityLoader
.DEFAULT
.loadClientIdentity(keyPath
,
84 FilePasswordProvider
.of(password
));
85 session
.addPublicKeyIdentity(keyPair
);
86 } catch (IOException
| GeneralSecurityException e
) {
87 throw new IllegalStateException(e
);
91 void openSession(URI uri
) {
92 openSession(uri
.getUserInfo(), uri
.getHost(), uri
.getPort() > 0 ? uri
.getPort() : null);
95 void openSession(String login
, String host
, Integer port
) {
97 throw new IllegalStateException("Session is already open");
104 login
= System
.getProperty("user.name");
105 String password
= null;
106 int sepIndex
= login
.indexOf(':');
108 if (sepIndex
+ 1 < login
.length()) {
109 password
= login
.substring(sepIndex
+ 1);
110 login
= login
.substring(0, sepIndex
);
112 throw new IllegalArgumentException("Illegal authority: " + login
);
115 ConnectFuture connectFuture
= getSshClient().connect(login
, host
, port
);
116 connectFuture
.await();
117 ClientSession session
= connectFuture
.getSession();
118 if (password
!= null) {
119 session
.addPasswordIdentity(password
);
122 this.session
= session
;
123 } catch (IOException e
) {
124 throw new IllegalStateException("Cannot connect to " + host
+ ":" + port
);
128 void closeSession() {
130 throw new IllegalStateException("No session is open");
133 } catch (IOException e
) {
140 ClientSession
getSession() {