├── LICENSE ├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── github │ │ └── woostju │ │ └── ssh │ │ ├── SshClient.java │ │ ├── SshClientConfig.java │ │ ├── SshClientEventListener.java │ │ ├── SshClientFactory.java │ │ ├── SshClientSSHJ.java │ │ ├── SshClientState.java │ │ ├── SshResponse.java │ │ ├── autoconfigure │ │ ├── SshClientPoolAutoConfiguration.java │ │ └── SshClientPoolProperties.java │ │ ├── exception │ │ ├── AuthException.java │ │ ├── LostConnectionException.java │ │ ├── SshException.java │ │ └── TimeoutException.java │ │ └── pool │ │ ├── SshClientPoolConfig.java │ │ ├── SshClientWrapper.java │ │ └── SshClientsPool.java └── resources │ └── META-INF │ └── spring.factories └── test └── java ├── com └── github │ └── test │ ├── CustomerSshClient.java │ ├── TestSshClient.java │ ├── TestSshClientsPool.java │ └── multithread │ ├── JobExecutor.java │ ├── JobExecutorState.java │ └── SyncThreadPool.java ├── id_rsa └── id_rsa.pub /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ssh-client-pool 2 | a java implementation of ssh clients object pool with [sshj](https://github.com/hierynomus/sshj), [apache common pool2](https://github.com/apache/commons-pool), [expectIt](https://github.com/Alexey1Gavrilov/ExpectIt) 3 | 4 | 5 | 6 | ## usage 7 | 8 | ssh-client-pool is available from **Maven Central** 9 | 10 | ```xml 11 | 12 | com.github.woostju 13 | ssh-client-pool 14 | 1.0.1-RELEASE 15 | 16 | ``` 17 | 18 | ### Who is this for? 19 | 20 | Anyone who wants to connect server instances through SSH, send commands to server and consume the output continuously. 21 | 22 | Anyone who wants to cache the connected clients in an object pool, to reuse the client. 23 | 24 | 25 | ### How do I use this? 26 | 27 | The SshClientsPool is auto-configured, use it directly as: 28 | 29 | ```java 30 | 31 | @Autowired 32 | SshClientsPool pool; 33 | 34 | public void echo(){ 35 | SshClientConfig clientConfig = new SshClientConfig("hostip", 22, "username", "password", null); 36 | SshClientWrapper client = pool.client(clientConfig); 37 | SshResponse response = client.executeCommand("echo 'hi'", 100); 38 | return response; 39 | } 40 | 41 | ``` 42 | 43 | ### Can I configure the pool? 44 | 45 | You can configure SshClientsPool in your SpringBoot properties file as: 46 | 47 | ``` 48 | woostju.ssh-client-pool.maxActive=16 49 | woostju.ssh-client-pool.maxIdle=16 50 | woostju.ssh-client-pool.idleTime=20000 51 | woostju.ssh-client-pool.maxWait=20000 52 | ``` 53 | 54 | what's more, you can also register the SshClientsPool yourself: 55 | 56 | ```java 57 | @Bean 58 | public SshClientsPool sshclientpool() { 59 | SshClientPoolConfig poolConfig = SshClientPoolConfig(); 60 | poolConfig.setMaxTotalPerKey(maxTotal); 61 | poolConfig.setMaxIdlePerKey(maxIdle); 62 | poolConfig.setBlockWhenExhausted(true); 63 | poolConfig.setMaxWaitMillis(1000L * maxWaitMillis); 64 | 65 | poolConfig.setMinEvictableIdleTimeMillis(1000L * idleTime); 66 | poolConfig.setTimeBetweenEvictionRunsMillis(1000L * idleTime); 67 | poolConfig.setTestOnBorrow(true); 68 | poolConfig.setTestOnReturn(true); 69 | poolConfig.setTestWhileIdle(true); 70 | poolConfig.setJmxEnabled(false); //disbale jmx 71 | 72 | return new SshClientsPool(poolConfig); 73 | } 74 | ``` 75 | 76 | SshClientPoolConfig is a subclass of GenericKeyedObjectPool in Apacha Common Pool2, learn more from [apache common pool2](https://github.com/apache/commons-pool) to configure the pool. 77 | 78 | ### How does it work? 79 | 80 | When you request a client from pool, it will pull an idle one, if there is no idle client, a created one return. 81 | After you execute a command, it will return the client to pool as an idle one. 82 | If you close the client explicitly, the client will be destroyed and remove from pool. 83 | 84 | ## License 85 | 86 | This code is under the [Apache Licence v2](https://www.apache.org/licenses/LICENSE-2.0). 87 | 88 | 89 | ## Additional Resources 90 | 91 | * [hierynomus/sshj](https://github.com/hierynomus/sshj) ssh, scp and sftp for java 92 | * [apache common pool2](https://github.com/apache/commons-pool) The Apache Commons Object Pooling Library. 93 | * [Alexey1Gavrilov/expectIt](https://github.com/Alexey1Gavrilov/ExpectIt) ExpectIt - is yet another pure Java 1.6+ implementation of the Expect tool. It is designed to be simple, easy to use and extensible. Written from scratch. 94 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | com.github.woostju 6 | ssh-client-pool 7 | jar 8 | 1.0.1-RELEASE 9 | ${project.groupId}:${project.artifactId} 10 | https://github.com/woostju/ssh-client-pool 11 | pool for java ssh client 12 | 13 | 14 | 15 | The Apache Software License, Version 2.0 16 | http://www.apache.org/licenses/LICENSE-2.0.txt 17 | repo 18 | 19 | 20 | 21 | 22 | 23 | James WU 24 | woostju@163.com 25 | 26 | 27 | 28 | 29 | scm:git@github.com:woostju/ssh-client-pool.git 30 | scm:git@github.com:woostju/ssh-client-pool.git 31 | https://github.com/woostju/ssh-client-pool.git 32 | 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-parent 38 | 1.5.10.RELEASE 39 | 40 | 41 | 42 | UTF-8 43 | UTF-8 44 | 1.8 45 | 46 | 47 | 48 | 49 | com.hierynomus 50 | sshj 51 | 0.26.0 52 | 53 | 54 | org.bouncycastle 55 | bcprov-jdk15on 56 | 1.60 57 | 58 | 59 | com.fasterxml.uuid 60 | java-uuid-generator 61 | 3.1.4 62 | 63 | 64 | net.sf.expectit 65 | expectit-core 66 | 0.9.0 67 | 68 | 69 | 70 | org.apache.commons 71 | commons-pool2 72 | 2.8.0 73 | 74 | 75 | org.springframework.boot 76 | spring-boot-starter-test 77 | 78 | 79 | 80 | 81 | 82 | 83 | org.apache.maven.plugins 84 | maven-source-plugin 85 | 86 | 87 | attach-sources 88 | 89 | jar-no-fork 90 | 91 | 92 | 93 | 94 | 95 | org.apache.maven.plugins 96 | maven-javadoc-plugin 97 | 98 | 99 | attach-javadocs 100 | 101 | jar 102 | 103 | 104 | 105 | 106 | 107 | org.apache.maven.plugins 108 | maven-gpg-plugin 109 | 1.5 110 | 111 | 112 | sign-artifacts 113 | verify 114 | 115 | sign 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | src/main/java 124 | 125 | **/*.properties 126 | **/*.xml 127 | 128 | false 129 | 130 | 131 | src/main/resources 132 | 133 | **/* 134 | 135 | false 136 | 137 | 138 | 139 | 140 | 141 | release 142 | 143 | 144 | 145 | org.sonatype.plugins 146 | nexus-staging-maven-plugin 147 | 1.6.3 148 | true 149 | 150 | ossrh 151 | https://oss.sonatype.org/ 152 | false 153 | 154 | 155 | 156 | org.apache.maven.plugins 157 | maven-release-plugin 158 | 2.5 159 | 160 | true 161 | false 162 | release 163 | deploy 164 | 165 | 166 | 167 | org.apache.maven.plugins 168 | maven-compiler-plugin 169 | 3.0 170 | 171 | 1.8 172 | 1.8 173 | 174 | 175 | 176 | org.apache.maven.plugins 177 | maven-gpg-plugin 178 | 1.5 179 | 180 | 181 | sign-artifacts 182 | verify 183 | 184 | sign 185 | 186 | 187 | 188 | 189 | 190 | org.apache.maven.plugins 191 | maven-source-plugin 192 | 2.2.1 193 | 194 | 195 | attach-sources 196 | 197 | jar-no-fork 198 | 199 | 200 | 201 | 202 | 203 | org.apache.maven.plugins 204 | maven-javadoc-plugin 205 | 2.9 206 | 207 | 208 | attach-javadocs 209 | 210 | jar 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | ossrh 222 | https://oss.sonatype.org/content/repositories/snapshots 223 | 224 | 225 | ossrh 226 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 227 | 228 | 229 | 230 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/SshClient.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh; 2 | 3 | import com.github.woostju.ssh.exception.SshException; 4 | 5 | /** 6 | * Ssh Client used to connect to server instance and execute command. The build-in implementation is {@link SshClientSSHJ}

7 | * 8 | * Client can be used in chain mode, {@link SshClient}.{@link #init(SshClientConfig)}.{@link #connect(int)}.{@link #authPassword()}.{@link #startSession(boolean)}.{@link #executeCommand(String, int)}

9 | * 10 | * At last, close the client with {@link #disconnect()} 11 | * 12 | *

Set an {@link SshClientEventListener} with {@link #setEventListener(SshClientEventListener)} to be notified when its event occurs 13 | *

14 | * @author jameswu 15 | * 16 | */ 17 | public interface SshClient { 18 | 19 | /** 20 | * pass the {@link SshClientConfig} to client 21 | * @param config the information used to connect to server 22 | * @return SshClient itself 23 | */ 24 | public SshClient init(SshClientConfig config); 25 | 26 | /** 27 | * connect to server, and timeout if longer than {@code timeoutInSeconds} 28 | * @param timeoutInSeconds timeout in seconds 29 | * @return SshClient itself 30 | * @throws SshException if server is unreachable, usually the host and port is incorrect 31 | */ 32 | public SshClient connect(int timeoutInSeconds) throws SshException; 33 | 34 | /** 35 | * auth with password 36 | * @return SshClient itself 37 | * @throws SshException if username or password is incorrect 38 | */ 39 | public SshClient authPassword() throws SshException; 40 | 41 | /** 42 | * auth with key 43 | * @return SshClient itself 44 | * @throws SshException if username or public key is incorrect 45 | */ 46 | public SshClient authPublickey() throws SshException; 47 | 48 | /** 49 | * start session 50 | * @param shellMode true: communicate with server interactively in session, just like command line 51 | *

false: only execute command once in session 52 | * @return SshClient itself 53 | * @throws SshException when start session failed 54 | * 55 | */ 56 | public SshClient startSession(boolean shellMode) throws SshException; 57 | 58 | /** 59 | * 60 | * @param command execute the {@code command} on server instance, and timeout if longer than {@code timeoutInSeconds}. 61 | * @param timeoutInSeconds timeout in seconds 62 | * @return SshResponse 63 | * 64 | */ 65 | public SshResponse executeCommand(String command, int timeoutInSeconds); 66 | 67 | /** 68 | * set the listener on SshClient 69 | * @param listener notify listener when events occur in SshClient 70 | * @return SshClient itself 71 | */ 72 | public SshClient setEventListener(SshClientEventListener listener); 73 | 74 | /** 75 | * disconnect from server 76 | */ 77 | public void disconnect(); 78 | 79 | /** 80 | * state of SshClient 81 | * 82 | * @return SshClientState the state of ssh client 83 | *

inited before {@link #startSession(boolean)} success 84 | *

connected after {@link #startSession(boolean)} success 85 | *

disconnected after {@link #disconnect()}, or any connection problem occurs 86 | */ 87 | public SshClientState getState(); 88 | 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/SshClientConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh; 2 | 3 | /** 4 | * 5 | * Configuration used by {@link SshClient} to connect to remote server instance 6 | * 7 | * @author jameswu 8 | * 9 | */ 10 | public class SshClientConfig { 11 | private String host; 12 | private int port; 13 | private String username; 14 | private String password; 15 | private String privateKeyPath; 16 | private String id; 17 | 18 | /** 19 | * 20 | * @return host address 21 | */ 22 | public String getHost() { 23 | return host; 24 | } 25 | 26 | /** 27 | * @param host host address, usually the ip address of remote server 28 | */ 29 | public void setHost(String host) { 30 | this.host = host; 31 | } 32 | 33 | /** 34 | * 35 | * @return ssh port of the remote server 36 | */ 37 | public int getPort() { 38 | return port; 39 | } 40 | 41 | /** 42 | * @param port ssh port of the remote server 43 | */ 44 | public void setPort(int port) { 45 | this.port = port; 46 | } 47 | 48 | /** 49 | * 50 | * @return ssh username of the remote server 51 | */ 52 | public String getUsername() { 53 | return username; 54 | } 55 | 56 | /** 57 | * 58 | * @param username ssh username of the remote server 59 | */ 60 | public void setUsername(String username) { 61 | this.username = username; 62 | } 63 | 64 | /** 65 | * 66 | * @return ssh password of the remote server 67 | */ 68 | public String getPassword() { 69 | return password; 70 | } 71 | 72 | /** 73 | * 74 | * @param password ssh password of the remote server 75 | */ 76 | public void setPassword(String password) { 77 | this.password = password; 78 | } 79 | 80 | /** 81 | * @return ssh local key file path of the remote server 82 | */ 83 | public String getPrivateKeyPath() { 84 | return privateKeyPath; 85 | } 86 | 87 | /** 88 | * @param privateKeyPath local key file path of the remote server 89 | */ 90 | public void setPrivateKeyPath(String privateKeyPath) { 91 | this.privateKeyPath = privateKeyPath; 92 | } 93 | 94 | /** 95 | * 96 | * @return id of the config 97 | */ 98 | public String getId() { 99 | return id; 100 | } 101 | 102 | /** 103 | * 104 | * @param host server host address 105 | * @param port server ssh port 106 | * @param username server ssh username 107 | * @param password server ssh password 108 | * @param privateKeyPath local security key used to connect to server 109 | */ 110 | public SshClientConfig(String host, int port, String username, String password, String privateKeyPath) { 111 | this.id = host + port + username; 112 | if (null != password && password.length() > 0) { 113 | this.id += password; 114 | } 115 | if (privateKeyPath != null) { 116 | this.id += privateKeyPath; 117 | } 118 | this.host = host; 119 | this.port = port; 120 | this.username = username; 121 | this.password = password; 122 | this.privateKeyPath = privateKeyPath; 123 | } 124 | 125 | public SshClientConfig() { 126 | 127 | } 128 | 129 | @Override 130 | public boolean equals(Object obj) { 131 | if (obj instanceof SshClientConfig) { 132 | return id.equals(((SshClientConfig) obj).getId()); 133 | } 134 | return false; 135 | } 136 | 137 | @Override 138 | public int hashCode() { 139 | return id.hashCode(); 140 | } 141 | 142 | @Override 143 | public String toString() { 144 | return this.id; 145 | } 146 | } -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/SshClientEventListener.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh; 2 | 3 | /** 4 | * 5 | * Set listener to a SshClient by {@link SshClient#setEventListener(SshClientEventListener)} 6 | * @author jameswu 7 | * 8 | */ 9 | public interface SshClientEventListener { 10 | 11 | /** 12 | * after SshClient finished executing command 13 | * @param client the ssh client 14 | */ 15 | public void didExecuteCommand(Object client); 16 | 17 | /** 18 | * after SshClient disconnnect from the remote server 19 | * @param client the ssh client 20 | */ 21 | public void didDisConnected(Object client); 22 | 23 | /** 24 | * after SshClient start the ssh session 25 | * @param client the ssh client 26 | */ 27 | public void didConnected(Object client); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/SshClientFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh; 2 | 3 | import com.github.woostju.ssh.pool.SshClientPoolConfig; 4 | 5 | /** 6 | * 7 | * Factory of {@link SshClient} implementation 8 | *

Create a new instance of {@link SshClientSSHJ} with {@link #newInstance(SshClientConfig)} 9 | *

Create a custom implementation of {@link SshClient} with {@link #newInstance(SshClientConfig, SshClientPoolConfig)} 10 | * 11 | * @author jameswu 12 | * 13 | */ 14 | public class SshClientFactory { 15 | 16 | /** 17 | * Create a new instance of {@link SshClientSSHJ} 18 | * @param config ssh connection configuration of the remote server 19 | * @return SshClient in inited state 20 | */ 21 | public static SshClient newInstance(SshClientConfig config){ 22 | return newInstance(config, null); 23 | } 24 | 25 | /** 26 | * Create a custom implementation of {@link SshClient} 27 | * @param config ssh connection configuration of the remote server 28 | * @param poolConfig customized configuration 29 | * @return SshClient in inited state 30 | * @throws RuntimeException if SshClientImplClass in {@code poolConfig} is invalid 31 | */ 32 | public static SshClient newInstance(SshClientConfig config, SshClientPoolConfig poolConfig){ 33 | try { 34 | SshClient client = null; 35 | if (poolConfig==null || poolConfig.getSshClientImplClass()==null){ 36 | client = new SshClientSSHJ(); 37 | }else { 38 | client = (SshClient)poolConfig.getSshClientImplClass().newInstance(); 39 | } 40 | client.init(config); 41 | if(client instanceof SshClientSSHJ && poolConfig!=null && poolConfig.getServerCommandPromotRegex()!=null) { 42 | ((SshClientSSHJ)client).setCommandPromotRegexStr(poolConfig.getServerCommandPromotRegex()); 43 | } 44 | return client; 45 | } catch (InstantiationException e) { 46 | throw new RuntimeException("new instance failed", e); 47 | } catch (IllegalAccessException e) { 48 | throw new RuntimeException("new instance failed", e); 49 | } 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/SshClientSSHJ.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.net.SocketException; 7 | import java.nio.channels.ClosedByInterruptException; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.concurrent.TimeUnit; 11 | 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | 15 | import com.github.woostju.ssh.exception.AuthException; 16 | import com.github.woostju.ssh.exception.LostConnectionException; 17 | import com.github.woostju.ssh.exception.SshException; 18 | import com.github.woostju.ssh.exception.TimeoutException; 19 | import com.github.woostju.ssh.pool.SshClientPoolConfig; 20 | 21 | import net.schmizz.sshj.DefaultConfig; 22 | import net.schmizz.sshj.SSHClient; 23 | import net.schmizz.sshj.connection.channel.direct.Session; 24 | import net.schmizz.sshj.connection.channel.direct.Session.Command; 25 | import net.schmizz.sshj.connection.channel.direct.Session.Shell; 26 | import net.schmizz.sshj.transport.TransportException; 27 | import net.schmizz.sshj.transport.verification.PromiscuousVerifier; 28 | import net.schmizz.sshj.userauth.keyprovider.KeyProvider; 29 | import net.sf.expectit.Expect; 30 | import net.sf.expectit.ExpectBuilder; 31 | import net.sf.expectit.ExpectIOException; 32 | import net.sf.expectit.Result; 33 | import net.sf.expectit.matcher.Matcher; 34 | 35 | import static net.sf.expectit.filter.Filters.removeColors; 36 | import static net.sf.expectit.filter.Filters.removeNonPrintable; 37 | import static net.sf.expectit.matcher.Matchers.regexp; 38 | import static net.sf.expectit.matcher.Matchers.contains; 39 | 40 | /** 41 | * 42 | * build-in {@link SshClient} implementation with hierynomus/SshJ 43 | * 44 | *

Trouble and shooting: 45 | *

Problem: {@link #authPublickey()} throw exceptions contains "net.schmizz.sshj.common.Buffer$BufferException:Bad item length" 46 | *

Solution: may caused by key file format issue,use ssh-keygen on a remote Linux server to generate the key 47 | * 48 | * 49 | * @author jameswu 50 | * 51 | */ 52 | public class SshClientSSHJ implements SshClient { 53 | 54 | private final static Logger logger = LoggerFactory.getLogger(SshClientSSHJ.class); 55 | 56 | private SshClientConfig clientConfig; 57 | 58 | private SSHClient client; 59 | 60 | private Expect expect = null; 61 | 62 | private Session session = null; 63 | 64 | private Shell shell = null; 65 | 66 | private boolean shellMode = false; 67 | 68 | private SshClientState state = SshClientState.inited; 69 | 70 | private SshClientEventListener eventListener; 71 | 72 | public String commandPromotRegexStr = "[\\[]?.+@.+~[\\]]?[#\\$] *"; 73 | 74 | public Matcher commandPromotRegex = regexp(commandPromotRegexStr); 75 | 76 | // initialize DefaultConfig will consume resources, so we should cache it 77 | private static DefaultConfig defaultConfig = null; 78 | 79 | public static DefaultConfig getDefaultConfig() { 80 | if(defaultConfig==null) { 81 | defaultConfig = new DefaultConfig(); 82 | } 83 | return defaultConfig; 84 | } 85 | 86 | /** 87 | * used in shell mode, once it start session with server, the server will return promot to client 88 | *

the promot looks like [centos@ip-172-31-31-82 ~]$ 89 | *

if the build-in one does not fit, you can change it by {@link SshClientPoolConfig#setServerCommandPromotRegex(String)} 90 | * @param promot used to match promot from server 91 | */ 92 | public void setCommandPromotRegexStr(String promot) { 93 | this.commandPromotRegexStr = promot; 94 | this.commandPromotRegex = regexp(this.commandPromotRegexStr); 95 | } 96 | 97 | @Override 98 | public SshClient init(SshClientConfig config) { 99 | this.clientConfig = config; 100 | return this; 101 | } 102 | 103 | private void validate() throws SshException{ 104 | if(this.clientConfig == null) { 105 | throw new SshException("missing client config"); 106 | } 107 | } 108 | 109 | @Override 110 | public SshClient connect(int timeoutInSeconds) throws SshException{ 111 | this.validate(); 112 | if (timeoutInSeconds <= 0) { 113 | timeoutInSeconds = Integer.MAX_VALUE; 114 | } else { 115 | timeoutInSeconds = timeoutInSeconds * 1000; 116 | } 117 | return this.connect(timeoutInSeconds, false); 118 | } 119 | 120 | private SshClient connect(int timeoutInSeconds, boolean retry) throws SshException{ 121 | logger.debug("connecting to " + this.clientConfig.getHost() + " port:" + this.clientConfig.getPort() + " timeout in:" 122 | + (timeoutInSeconds / 1000) + " s"); 123 | client = new SSHClient(getDefaultConfig()); 124 | try { 125 | client.setConnectTimeout(timeoutInSeconds); 126 | client.addHostKeyVerifier(new PromiscuousVerifier()); 127 | // client.loadKnownHosts(); 128 | client.connect(this.clientConfig.getHost().trim(), this.clientConfig.getPort()); 129 | logger.debug("connected to " + this.clientConfig.getHost().trim() + " port:" + this.clientConfig.getPort()); 130 | } catch (TransportException e) { 131 | if(!retry) { 132 | logger.error("sshj get exception when connect and will retry one more time ", e); 133 | try { 134 | Thread.sleep(1000); 135 | } catch (InterruptedException e1) { 136 | } 137 | return this.connect(timeoutInSeconds, true); 138 | }else { 139 | String errorMessage ="connect to " + this.clientConfig.getHost().trim() + " failed"; 140 | logger.error(errorMessage, e); 141 | throw new SshException(errorMessage, e); 142 | } 143 | } catch (Exception e) { 144 | String errorMessage ="connect to " + this.clientConfig.getHost().trim() + " failed"; 145 | logger.error(errorMessage, e); 146 | throw new SshException(errorMessage, e); 147 | } 148 | return this; 149 | } 150 | 151 | @Override 152 | public SshClient setEventListener(SshClientEventListener listener) { 153 | this.eventListener = listener; 154 | return this; 155 | } 156 | 157 | @Override 158 | public SshClient authPassword() throws SshException { 159 | try { 160 | logger.debug("auth with password"); 161 | client.authPassword(this.clientConfig.getUsername(), this.clientConfig.getPassword()); 162 | } catch (Exception e) { 163 | String errorMessage = "ssh auth " + this.clientConfig.getHost() + " fail"; 164 | logger.error(errorMessage, e); 165 | throw new AuthException(errorMessage, e); 166 | } 167 | return this; 168 | } 169 | 170 | @Override 171 | public SshClient authPublickey() throws SshException { 172 | try { 173 | logger.debug("auth with key:"+this.clientConfig.getUsername()+","+this.clientConfig.getPrivateKeyPath()); 174 | if (this.clientConfig.getPrivateKeyPath() != null) { 175 | KeyProvider keys = client.loadKeys(this.clientConfig.getPrivateKeyPath()); 176 | client.authPublickey(this.clientConfig.getUsername(), keys); 177 | } else { 178 | client.authPublickey(this.clientConfig.getUsername()); 179 | } 180 | } catch (Exception e) { 181 | String errorMessage = "ssh auth " + this.clientConfig.getHost() + " fail"; 182 | logger.error(errorMessage, e); 183 | throw new AuthException(errorMessage, e); 184 | } 185 | return this; 186 | } 187 | 188 | @Override 189 | public SshClient startSession(boolean shellMode) { 190 | logger.info("start session " + (shellMode ? " in shellMode" : "")); 191 | try { 192 | session = client.startSession(); 193 | this.shellMode = shellMode; 194 | if (shellMode) { 195 | session.allocateDefaultPTY(); 196 | shell = session.startShell(); 197 | shell.changeWindowDimensions(1024, 1024, 20, 20); 198 | this.renewExpect(60); 199 | expect.expect(commandPromotRegex); 200 | } 201 | this.state = SshClientState.connected; 202 | try { 203 | if(this.eventListener!=null) { 204 | this.eventListener.didConnected(this); 205 | } 206 | } catch (Exception e) { 207 | } 208 | } catch (Exception e) { 209 | if(e instanceof ExpectIOException) { 210 | ExpectIOException ioException = (ExpectIOException)e; 211 | logger.error("start session fail with server input:"+ioException.getInputBuffer().replaceAll("[\\\n\\\r]", ""), e); 212 | }else { 213 | logger.error("start session fail", e); 214 | } 215 | this.disconnect(); 216 | throw new RuntimeException("start session fail." + e.getMessage()); 217 | } finally { 218 | // close expect 219 | try { 220 | if (expect != null) { 221 | expect.close(); 222 | } 223 | } catch (IOException e) { 224 | logger.error("close IO error", e); 225 | } 226 | expect = null; 227 | } 228 | return this; 229 | } 230 | 231 | @Override 232 | public SshResponse executeCommand(String command, int timeoutInSeconds) { 233 | if (this.shellMode) { 234 | return this.sendCommand(command, timeoutInSeconds); 235 | } else { 236 | return this.executeCommand_(command, timeoutInSeconds); 237 | } 238 | } 239 | 240 | private SshResponse executeCommand_(String command, int timeoutInSeconds) { 241 | logger.info("execute command: " + command); 242 | SshResponse response = new SshResponse(); 243 | try { 244 | Command cmd = session.exec(command); 245 | if (timeoutInSeconds < 0) { 246 | cmd.join(Long.MAX_VALUE, TimeUnit.SECONDS); 247 | } else { 248 | cmd.join(timeoutInSeconds, TimeUnit.SECONDS); 249 | } 250 | BufferedReader reader = new BufferedReader(new InputStreamReader(cmd.getInputStream(), "UTF-8")); 251 | BufferedReader error_reader = new BufferedReader(new InputStreamReader(cmd.getErrorStream(), "UTF-8")); 252 | List outputLines = new ArrayList<>(); 253 | logger.debug("finish executing command on " + this.clientConfig.getHost() + ", console:"); 254 | String outputLine; 255 | while ((outputLine = error_reader.readLine()) != null) { 256 | logger.debug(outputLine); 257 | outputLines.add(outputLine); 258 | } 259 | while ((outputLine = reader.readLine()) != null) { 260 | logger.debug(outputLine); 261 | outputLines.add(outputLine); 262 | } 263 | response.setStdout(outputLines); 264 | logger.info( 265 | "execute ssh command on " + this.clientConfig.getHost() + " completed, with exit status:" + cmd.getExitStatus()); 266 | response.setCode(cmd.getExitStatus()); 267 | } catch (Exception e) { 268 | if (e.getCause() instanceof InterruptedException || e.getCause() instanceof java.util.concurrent.TimeoutException) { 269 | logger.error("execute ssh on " + this.clientConfig.getHost() + " timeout"); 270 | response.setException(new TimeoutException("execute ssh command timeout")); 271 | } else { 272 | logger.error("execute ssh on " + this.clientConfig.getHost() + ", command error", e); 273 | response.setException(new SshException("execute ssh command error "+e.getMessage())); 274 | } 275 | }finally { 276 | try { 277 | if(this.eventListener!=null) { 278 | this.eventListener.didExecuteCommand(this); 279 | } 280 | } catch (Exception e) { 281 | } 282 | } 283 | return response; 284 | } 285 | 286 | private SshResponse sendCommand(String command, int timeoutInSeconds) { 287 | SshResponse response = new SshResponse(); 288 | if (this.state != SshClientState.connected) { 289 | response.setException(new LostConnectionException("client not connected")); 290 | response.setCode(0); 291 | return response; 292 | } 293 | try { 294 | this.renewExpect(timeoutInSeconds); 295 | // start expect 296 | logger.info(this + " execute command : " + command); 297 | expect.send(command); 298 | logger.debug(this + " command sent "); 299 | if (!command.endsWith("\n")) { 300 | expect.send("\n"); 301 | logger.debug(this + " command \\n sent "); 302 | } 303 | Result result2 = expect.expect(contains(command)); 304 | Result result = expect.expect(commandPromotRegex); 305 | logger.debug("command execute success with raw output"); 306 | logger.debug("------------------------------------------"); 307 | String[] inputArray = result.getInput().split("\\r\\n"); 308 | List stout = new ArrayList(); 309 | if(inputArray.length>0) { 310 | for(int i=0;i stdout = new ArrayList(); 20 | 21 | /** 22 | * @return 0 23 | */ 24 | public int getCode() { 25 | return code; 26 | } 27 | 28 | public void setCode(int code) { 29 | this.code = code; 30 | } 31 | 32 | /** 33 | * 34 | * @return the exception in {@link SshClient#executeCommand(String, int)} 35 | */ 36 | public Exception getException() { 37 | return exception; 38 | } 39 | 40 | public void setException(Exception exception) { 41 | this.exception = exception; 42 | } 43 | 44 | /** 45 | * 46 | * @return the output from remote server after send command 47 | */ 48 | public List getStdout() { 49 | return stdout; 50 | } 51 | 52 | public void setStdout(List stdout) { 53 | this.stdout = stdout; 54 | } 55 | 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/autoconfigure/SshClientPoolAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh.autoconfigure; 2 | 3 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 4 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | import com.github.woostju.ssh.pool.SshClientPoolConfig; 9 | import com.github.woostju.ssh.pool.SshClientsPool; 10 | 11 | @Configuration 12 | @EnableConfigurationProperties(SshClientPoolProperties.class) 13 | public class SshClientPoolAutoConfiguration { 14 | 15 | private final SshClientPoolProperties properties; 16 | 17 | public SshClientPoolAutoConfiguration(SshClientPoolProperties properties) { 18 | this.properties = properties; 19 | } 20 | 21 | @Bean 22 | @ConditionalOnMissingBean(SshClientsPool.class) 23 | SshClientsPool sshClientsPool() { 24 | return new SshClientsPool(sshClientPoolConfig()); 25 | } 26 | 27 | SshClientPoolConfig sshClientPoolConfig() { 28 | SshClientPoolConfig poolConfig = new SshClientPoolConfig(properties.getMaxActive() 29 | ,properties.getMaxIdle() 30 | ,properties.getIdleTime() 31 | ,properties.getMaxWait()); 32 | if(properties.getSshj()!=null) { 33 | poolConfig.setServerCommandPromotRegex(properties.getSshj().getServerCommandPromotRegex()); 34 | } 35 | if (properties.getSshClientImplClass()!=null) { 36 | try { 37 | poolConfig.setSshClientImplClass(Class.forName(properties.getSshClientImplClass())); 38 | } catch (ClassNotFoundException e) { 39 | e.printStackTrace(); 40 | } 41 | } 42 | return poolConfig; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/autoconfigure/SshClientPoolProperties.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh.autoconfigure; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | @ConfigurationProperties("woostju.ssh-client-pool") 6 | public class SshClientPoolProperties { 7 | 8 | /** 9 | * Max number of "idle" connections in the pool. Use a negative value to indicate 10 | * an unlimited number of idle connections. 11 | */ 12 | private int maxIdle = 20; 13 | 14 | /** 15 | * 16 | */ 17 | private int idleTime = 120*1000; 18 | 19 | /** 20 | * Max number of connections that can be allocated by the pool at a given time. 21 | * Use a negative value for no limit. 22 | */ 23 | private int maxActive = 20; 24 | 25 | /** 26 | * Maximum amount of time (in milliseconds) a connection allocation should block 27 | * before throwing an exception when the pool is exhausted. Use a negative value 28 | * to block indefinitely. 29 | */ 30 | private int maxWait = 120*1000; 31 | 32 | private String sshClientImplClass = "com.github.woostju.ssh.SshClientSSHJ"; 33 | 34 | private SshClientProperites sshj; 35 | 36 | 37 | public int getMaxIdle() { 38 | return maxIdle; 39 | } 40 | 41 | 42 | public void setMaxIdle(int maxIdle) { 43 | this.maxIdle = maxIdle; 44 | } 45 | 46 | 47 | 48 | public int getIdleTime() { 49 | return idleTime; 50 | } 51 | 52 | 53 | 54 | public void setIdleTime(int idleTime) { 55 | this.idleTime = idleTime; 56 | } 57 | 58 | 59 | 60 | public int getMaxActive() { 61 | return maxActive; 62 | } 63 | 64 | 65 | 66 | public void setMaxActive(int maxActive) { 67 | this.maxActive = maxActive; 68 | } 69 | 70 | 71 | 72 | public int getMaxWait() { 73 | return maxWait; 74 | } 75 | 76 | 77 | 78 | public void setMaxWait(int maxWait) { 79 | this.maxWait = maxWait; 80 | } 81 | 82 | 83 | 84 | public String getSshClientImplClass() { 85 | return sshClientImplClass; 86 | } 87 | 88 | 89 | 90 | public void setSshClientImplClass(String sshClientImplClass) { 91 | this.sshClientImplClass = sshClientImplClass; 92 | } 93 | 94 | 95 | 96 | public SshClientProperites getSshj() { 97 | return sshj; 98 | } 99 | 100 | 101 | 102 | public void setSshj(SshClientProperites sshj) { 103 | this.sshj = sshj; 104 | } 105 | 106 | 107 | 108 | public static class SshClientProperites{ 109 | private String serverCommandPromotRegex; 110 | 111 | public String getServerCommandPromotRegex() { 112 | return serverCommandPromotRegex; 113 | } 114 | 115 | public void setServerCommandPromotRegex(String serverCommandPromotRegex) { 116 | this.serverCommandPromotRegex = serverCommandPromotRegex; 117 | } 118 | 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/exception/AuthException.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh.exception; 2 | 3 | /** 4 | * Ssh auth failed 5 | * @author jameswu 6 | * 7 | */ 8 | public class AuthException extends SshException{ 9 | 10 | public AuthException(String message) { 11 | this(message, null); 12 | } 13 | 14 | public AuthException(String message, Throwable error) { 15 | super(message, error); 16 | } 17 | 18 | /** 19 | * 20 | */ 21 | private static final long serialVersionUID = -3961786667342327L; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/exception/LostConnectionException.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh.exception; 2 | 3 | /** 4 | * The ssh connection is disconnected 5 | * @author jameswu 6 | * 7 | */ 8 | public class LostConnectionException extends SshException{ 9 | 10 | 11 | private static final long serialVersionUID = -3961870786667342727L; 12 | 13 | public LostConnectionException(String message) { 14 | this(message, null); 15 | } 16 | 17 | public LostConnectionException(String message, Throwable error) { 18 | super(message, error); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/exception/SshException.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh.exception; 2 | 3 | public class SshException extends Exception{ 4 | 5 | /** 6 | * 7 | */ 8 | private static final long serialVersionUID = 2052615275027564490L; 9 | 10 | public SshException(String message, Throwable error) { 11 | super(message); 12 | if(error != null) { 13 | initCause(error); 14 | } 15 | } 16 | 17 | public SshException(String message) { 18 | this(message, null); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/exception/TimeoutException.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh.exception; 2 | 3 | 4 | /** 5 | * Timeout Exception 6 | * @author jameswu 7 | * 8 | */ 9 | public class TimeoutException extends SshException{ 10 | 11 | public TimeoutException(String message) { 12 | this(message, null); 13 | } 14 | 15 | public TimeoutException(String message, Throwable error) { 16 | super(message, error); 17 | } 18 | 19 | /** 20 | * 21 | */ 22 | private static final long serialVersionUID = -39618386667342727L; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/pool/SshClientPoolConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh.pool; 2 | 3 | 4 | import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig; 5 | 6 | import com.github.woostju.ssh.SshClient; 7 | import com.github.woostju.ssh.SshClientSSHJ; 8 | 9 | 10 | /** 11 | * 12 | * The configuration of SshClientPool library 13 | *

SshClientPoolConfig is a subclass of GenericKeyedObjectPoolConfig to control the pool behavior 14 | *

Also, you can replace the build-in {@link SshClient} implementation by {@link SshClientPoolConfig#setSshClientImplClass(Class)} if you want 15 | * 16 | * @author jameswu 17 | */ 18 | public class SshClientPoolConfig extends GenericKeyedObjectPoolConfig{ 19 | 20 | private Class sshClientImplClass; 21 | 22 | private String serverCommandPromotRegex; 23 | 24 | public SshClientPoolConfig() { 25 | super(); 26 | } 27 | 28 | /** 29 | * quick way to create SshClientPoolConfig 30 | * set TestOnBorrow to true 31 | * set TestOnReturn to true 32 | * set TestWhileIdle to true 33 | * set JmxEnabled to false 34 | * @param maxActive maxTotalPerKey 35 | * @param maxIdle maxIdlePerKey 36 | * @param idleTime idle time 37 | * @param maxWaitTime maxWaitMillis 38 | */ 39 | public SshClientPoolConfig(int maxActive, int maxIdle, long idleTime, long maxWaitTime){ 40 | this.setMaxTotalPerKey(maxActive); 41 | this.setMaxIdlePerKey(maxIdle); 42 | this.setMaxWaitMillis(maxWaitTime); 43 | this.setBlockWhenExhausted(true); 44 | this.setMinEvictableIdleTimeMillis(idleTime); 45 | this.setTimeBetweenEvictionRunsMillis(idleTime); 46 | this.setTestOnBorrow(true); 47 | this.setTestOnReturn(true); 48 | this.setTestWhileIdle(true); 49 | this.setJmxEnabled(false); 50 | } 51 | 52 | public Class getSshClientImplClass() { 53 | return sshClientImplClass; 54 | } 55 | 56 | /** 57 | * replace the build-in {@link SshClient} by {@link SshClientPoolConfig#setSshClientImplClass(Class)} 58 | * @param sshClientImplClass the implementation of {@link SshClient} 59 | */ 60 | public void setSshClientImplClass(Class sshClientImplClass) { 61 | this.sshClientImplClass = sshClientImplClass; 62 | } 63 | 64 | /** 65 | * 66 | * @return regex string used to match promot from server 67 | */ 68 | public String getServerCommandPromotRegex() { 69 | return serverCommandPromotRegex; 70 | } 71 | 72 | /** 73 | * see {@link SshClientSSHJ#setCommandPromotRegexStr(String)} 74 | * @param serverCommandPromotRegex regex string used to match promot from server 75 | */ 76 | public void setServerCommandPromotRegex(String serverCommandPromotRegex) { 77 | this.serverCommandPromotRegex = serverCommandPromotRegex; 78 | } 79 | 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/pool/SshClientWrapper.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh.pool; 2 | 3 | import java.util.UUID; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | import com.github.woostju.ssh.SshClient; 9 | import com.github.woostju.ssh.SshClientConfig; 10 | import com.github.woostju.ssh.SshClientEventListener; 11 | import com.github.woostju.ssh.SshClientFactory; 12 | import com.github.woostju.ssh.SshClientState; 13 | import com.github.woostju.ssh.SshResponse; 14 | import com.github.woostju.ssh.exception.SshException; 15 | 16 | /** 17 | * A wrapper class of {@link SshClient} used by {@link SshClientsPool} 18 | * 19 | * @author jameswu 20 | * 21 | */ 22 | public class SshClientWrapper implements SshClientEventListener{ 23 | 24 | private final static Logger logger = LoggerFactory.getLogger(SshClientWrapper.class); 25 | 26 | private String id; 27 | 28 | private SshClient client; 29 | 30 | SshClientEventListener listener; 31 | 32 | SshClientConfig config; 33 | 34 | public String getId() { 35 | return this.id; 36 | } 37 | 38 | public void setListener(SshClientEventListener listener) { 39 | this.listener = listener; 40 | } 41 | 42 | public SshClientConfig getConfig() { 43 | return this.config; 44 | } 45 | 46 | public SshClientWrapper(SshClientConfig config, SshClientPoolConfig poolConfig) { 47 | this.id = UUID.randomUUID().toString(); 48 | this.config = config; 49 | this.client = SshClientFactory.newInstance(config, poolConfig); 50 | } 51 | 52 | public SshClientWrapper setEventListener(SshClientEventListener listener) { 53 | this.listener = listener; 54 | this.client.setEventListener(this); 55 | return this; 56 | } 57 | 58 | public SshClientWrapper connect(int timeoutInSeconds) throws SshException { 59 | client.connect(timeoutInSeconds); 60 | return this; 61 | } 62 | 63 | public SshClientWrapper auth() throws SshException{ 64 | if(null!=this.config.getPassword() && this.config.getPassword().length()>0) { 65 | client.authPassword(); 66 | }else if(null!=this.config.getPrivateKeyPath() && this.config.getPrivateKeyPath().length()>0) { 67 | client.authPublickey(); 68 | }else { 69 | client.authPublickey(); 70 | } 71 | return this; 72 | } 73 | 74 | 75 | public SshClientWrapper startSession() throws SshException{ 76 | client.startSession(true); 77 | return this; 78 | } 79 | 80 | 81 | public SshResponse executeCommand(String command, int timeoutInSeconds){ 82 | SshResponse response = client.executeCommand(command, timeoutInSeconds); 83 | return response; 84 | } 85 | 86 | public void disconnect() { 87 | client.disconnect(); 88 | } 89 | 90 | public boolean equals(Object obj) { 91 | if(obj instanceof SshClientWrapper){ 92 | return id.equals(((SshClientWrapper)obj).getId()); 93 | } 94 | return false; 95 | } 96 | 97 | public int hashCode(){ 98 | return id.hashCode(); 99 | } 100 | 101 | public SshClientState getState() { 102 | return client.getState(); 103 | } 104 | 105 | public String toString() { 106 | return "["+this.id+"|" 107 | +this.config.getHost()+"|" 108 | +this.config.getPort()+"|" 109 | +this.getState()+"]"; 110 | } 111 | 112 | @Override 113 | public void didExecuteCommand(Object client) { 114 | this.listener.didExecuteCommand(this); 115 | } 116 | 117 | @Override 118 | public void didDisConnected(Object client) { 119 | this.listener.didDisConnected(this); 120 | } 121 | 122 | @Override 123 | public void didConnected(Object client) { 124 | this.listener.didConnected(this); 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /src/main/java/com/github/woostju/ssh/pool/SshClientsPool.java: -------------------------------------------------------------------------------- 1 | package com.github.woostju.ssh.pool; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import org.apache.commons.pool2.BaseKeyedPooledObjectFactory; 7 | import org.apache.commons.pool2.PooledObject; 8 | import org.apache.commons.pool2.impl.DefaultPooledObject; 9 | import org.apache.commons.pool2.impl.DefaultPooledObjectInfo; 10 | import org.apache.commons.pool2.impl.GenericKeyedObjectPool; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | 14 | import com.github.woostju.ssh.SshClientConfig; 15 | import com.github.woostju.ssh.SshClientEventListener; 16 | import com.github.woostju.ssh.SshClientState; 17 | import com.github.woostju.ssh.exception.SshException; 18 | 19 | /** 20 | * 21 | * SshClient objects pool 22 | *

Cache shell mode connected SshClient in the pools, to save connect time, also improve performance 23 | * 24 | * @author jameswu 25 | */ 26 | 27 | public class SshClientsPool extends GenericKeyedObjectPool{ 28 | private final static Logger logger = LoggerFactory.getLogger(SshClientsPool.class); 29 | 30 | private SshClientPoolConfig poolConfig; 31 | 32 | /** 33 | * maxActive 20, maxIdle 20, maxWaitMillis 40 seconds 34 | */ 35 | public SshClientsPool() { 36 | this(20, 20, 120*1000l, 120*1000l); 37 | } 38 | 39 | /** 40 | * 41 | * @param poolConfig create SshClientsPool with {@code poolConfig} 42 | */ 43 | public SshClientsPool(SshClientPoolConfig poolConfig) { 44 | super(new SshClientsObjectFactory(), poolConfig); 45 | ((SshClientsObjectFactory)this.getFactory()).setSshClientsPool(this); 46 | this.poolConfig = poolConfig; 47 | } 48 | 49 | /** 50 | * 51 | * @param maxActive max clients in pool 52 | * @param maxIdle max idle clients in pool 53 | * @param idleTime idle time clients live in the pool 54 | * @param maxWaitTime wait time when request block 55 | */ 56 | public SshClientsPool(int maxActive, int maxIdle, long idleTime, long maxWaitTime) { 57 | this(new SshClientPoolConfig(maxActive, maxIdle, idleTime, maxWaitTime)); 58 | } 59 | 60 | /** 61 | * request a connected client from pool, may be a cached one, maybe a brand-new one 62 | * @param config the connection information to host 63 | * @return SshClientWrapper 64 | */ 65 | public SshClientWrapper client(SshClientConfig config) { 66 | try { 67 | return this.borrowObject(config); 68 | } catch (Exception e) { 69 | logger.error("create ssh client error", e); 70 | throw new RuntimeException(e); 71 | } 72 | } 73 | 74 | public SshClientPoolConfig getPoolConfig() { 75 | return poolConfig; 76 | } 77 | 78 | /** 79 | * query objects with same server connection information 80 | * @param config server connection information 81 | * @return lists of DefaultPooledObjectInfo with SshClientWrapper inside 82 | */ 83 | public List getObjects(SshClientConfig config) { 84 | Map> objects = listAllObjects(); 85 | return objects.get(config.toString()); 86 | } 87 | 88 | @Override 89 | public SshClientWrapper borrowObject(SshClientConfig key) throws Exception { 90 | SshClientWrapper object = super.borrowObject(key); 91 | logger.debug("borrow object:" + object); 92 | return object; 93 | } 94 | 95 | } 96 | 97 | class SshClientsObjectFactory extends BaseKeyedPooledObjectFactory implements SshClientEventListener{ 98 | private final static Logger logger = LoggerFactory.getLogger(SshClientsObjectFactory.class); 99 | 100 | private SshClientsPool pool; 101 | 102 | public void setSshClientsPool(SshClientsPool pool) { 103 | this.pool = pool; 104 | } 105 | 106 | @Override 107 | public boolean validateObject(SshClientConfig key, PooledObject p) { 108 | return p.getObject().getState() == SshClientState.connected; 109 | } 110 | 111 | @Override 112 | public void destroyObject(SshClientConfig key, PooledObject p) throws Exception { 113 | logger.debug("destroy object: "+p); 114 | p.getObject().setEventListener(null); 115 | p.getObject().disconnect(); 116 | } 117 | 118 | @Override 119 | public void activateObject(SshClientConfig key, PooledObject p) throws Exception { 120 | super.activateObject(key, p); 121 | } 122 | 123 | @Override 124 | public PooledObject wrap(SshClientWrapper value) { 125 | return new DefaultPooledObject(value); 126 | } 127 | 128 | @Override 129 | public SshClientWrapper create(SshClientConfig config) { 130 | SshClientWrapper wrapper = new SshClientWrapper(config, pool.getPoolConfig()); 131 | try { 132 | wrapper.setEventListener(this).connect(60).auth().startSession(); 133 | } catch (SshException e) { 134 | throw new RuntimeException("create ssh client fail"); 135 | } 136 | logger.debug("sshclient created: "+wrapper); 137 | return wrapper; 138 | } 139 | 140 | @Override 141 | public void didExecuteCommand(Object client) { 142 | if(client instanceof SshClientWrapper) { 143 | SshClientWrapper wrapper = (SshClientWrapper)client; 144 | pool.returnObject(wrapper.getConfig(), wrapper); 145 | } 146 | } 147 | 148 | @Override 149 | public void didDisConnected(Object client) { 150 | if(client instanceof SshClientWrapper) { 151 | SshClientWrapper wrapper = (SshClientWrapper)client; 152 | try { 153 | pool.invalidateObject(wrapper.getConfig(), wrapper); 154 | } catch (Exception e) { 155 | logger.error("invalidate object "+client+" failed",e); 156 | } 157 | } 158 | } 159 | 160 | @Override 161 | public void didConnected(Object client) { 162 | 163 | } 164 | 165 | } 166 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 | com.github.woostju.ssh.autoconfigure.SshClientPoolAutoConfiguration -------------------------------------------------------------------------------- /src/test/java/com/github/test/CustomerSshClient.java: -------------------------------------------------------------------------------- 1 | package com.github.test; 2 | 3 | import com.github.woostju.ssh.SshClientSSHJ; 4 | import com.github.woostju.ssh.SshResponse; 5 | 6 | public class CustomerSshClient extends SshClientSSHJ{ 7 | 8 | @Override 9 | public SshResponse executeCommand(String command, int timeoutInSeconds) { 10 | SshResponse repsonse = super.executeCommand(command, timeoutInSeconds); 11 | repsonse.getStdout().add("success"); 12 | return repsonse; 13 | } 14 | } -------------------------------------------------------------------------------- /src/test/java/com/github/test/TestSshClient.java: -------------------------------------------------------------------------------- 1 | package com.github.test; 2 | 3 | import static org.junit.Assert.assertArrayEquals; 4 | 5 | import java.io.IOException; 6 | import java.util.stream.Collectors; 7 | 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import com.github.woostju.ssh.SshClientConfig; 12 | import com.github.woostju.ssh.SshClientFactory; 13 | import com.github.woostju.ssh.SshResponse; 14 | import com.github.woostju.ssh.exception.AuthException; 15 | import com.github.woostju.ssh.exception.SshException; 16 | import com.github.woostju.ssh.exception.TimeoutException; 17 | 18 | 19 | public class TestSshClient { 20 | 21 | private SshClientConfig clientConfig; 22 | 23 | private String host = "52.81.83.116"; 24 | 25 | @Before 26 | public void init() throws IOException { 27 | String path = this.getClass().getClassLoader().getResource("id_rsa").getPath(); 28 | clientConfig = new SshClientConfig(host, 22, "centos", null, path); 29 | } 30 | 31 | @Test 32 | public void testSuccess() throws SshException{ 33 | SshResponse response = SshClientFactory.newInstance(clientConfig).connect(100) 34 | .authPublickey().startSession(false) 35 | .executeCommand("echo 'yes'", 100); 36 | 37 | assertArrayEquals(new Object[]{ 38 | 0,true 39 | }, new Object[]{ 40 | response.getCode(), 41 | response.getStdout().stream().collect(Collectors.joining("")).contains("yes") 42 | }); 43 | } 44 | 45 | @Test(expected = SshException.class) 46 | public void testConnectToUnreachableHost() throws SshException{ 47 | clientConfig.setHost(clientConfig.getHost()+"1"); 48 | SshClientFactory.newInstance(clientConfig).connect(100); 49 | } 50 | 51 | @Test(expected = SshException.class) 52 | public void testConnectWithWrongPort() throws SshException{ 53 | clientConfig.setPort(33);; 54 | SshClientFactory.newInstance(clientConfig).connect(100); 55 | } 56 | 57 | @Test(expected = AuthException.class) 58 | public void testAuthWithWrongUser() throws SshException{ 59 | clientConfig.setUsername(clientConfig.getUsername()+"1"); 60 | SshClientFactory.newInstance(clientConfig).connect(100) 61 | .authPublickey(); 62 | } 63 | 64 | @Test 65 | public void testExecuteCommandTimeout() throws SshException{ 66 | SshResponse response = SshClientFactory.newInstance(clientConfig).connect(100) 67 | .authPublickey().startSession(false) 68 | .executeCommand("echo 'timeout' >> /home/b.txt; sleep 100s;", 60); 69 | assertArrayEquals(new Object[]{ 70 | true 71 | }, new Object[]{ 72 | response.getException() instanceof TimeoutException 73 | }); 74 | } 75 | 76 | @Test 77 | public void testExecuteWrongCommand() throws SshException{ 78 | SshResponse response = SshClientFactory.newInstance(clientConfig).connect(100) 79 | .authPublickey().startSession(false) 80 | .executeCommand("sleep 20s echo 'timeout'",100); 81 | assertArrayEquals(new Object[]{ 82 | 1 83 | }, new Object[]{ 84 | response.getCode() 85 | }); 86 | } 87 | 88 | @Test 89 | public void testSuccessWithShellmode() throws SshException{ 90 | SshResponse response = SshClientFactory.newInstance(clientConfig).connect(100) 91 | .authPublickey().startSession(true) 92 | .executeCommand("echo 'yes'", 100); 93 | 94 | assertArrayEquals(new Object[]{ 95 | 0,true 96 | }, new Object[]{ 97 | response.getCode(), 98 | response.getStdout().stream().collect(Collectors.joining("")).contains("yes") 99 | }); 100 | } 101 | 102 | @Test 103 | public void testExecuteCommandTimeoutWithShellmode() throws SshException{ 104 | SshResponse response = SshClientFactory.newInstance(clientConfig).connect(100) 105 | .authPublickey().startSession(true) 106 | .executeCommand("echo 'timeout' >> /home/b.txt; sleep 100s;", 60); 107 | assertArrayEquals(new Object[]{ 108 | true 109 | }, new Object[]{ 110 | response.getException() instanceof TimeoutException 111 | }); 112 | } 113 | 114 | @Test 115 | public void testExecuteWrongCommandWithShellmode() throws SshException{ 116 | SshResponse response = SshClientFactory.newInstance(clientConfig).connect(100) 117 | .authPublickey().startSession(true) 118 | .executeCommand("sleep 20s echo 'timeout'",100); 119 | assertArrayEquals(new Object[]{ 120 | 0 121 | }, new Object[]{ 122 | response.getCode() 123 | }); 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /src/test/java/com/github/test/TestSshClientsPool.java: -------------------------------------------------------------------------------- 1 | package com.github.test; 2 | 3 | import static org.junit.Assert.assertArrayEquals; 4 | 5 | import java.io.IOException; 6 | import java.util.HashSet; 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.Set; 10 | import java.util.stream.Collectors; 11 | 12 | import org.apache.commons.pool2.impl.DefaultPooledObjectInfo; 13 | import org.junit.After; 14 | import org.junit.Before; 15 | import org.junit.Test; 16 | 17 | import com.github.test.multithread.JobExecutor; 18 | import com.github.test.multithread.JobExecutorState; 19 | import com.github.test.multithread.SyncThreadPool; 20 | import com.github.woostju.ssh.SshClientConfig; 21 | import com.github.woostju.ssh.SshResponse; 22 | import com.github.woostju.ssh.pool.SshClientPoolConfig; 23 | import com.github.woostju.ssh.pool.SshClientWrapper; 24 | import com.github.woostju.ssh.pool.SshClientsPool; 25 | 26 | public class TestSshClientsPool { 27 | 28 | private SshClientConfig clientConfig; 29 | 30 | private String host = "52.81.83.116"; 31 | 32 | @Before 33 | public void init() throws IOException { 34 | String path = this.getClass().getClassLoader().getResource("id_rsa").getPath(); 35 | clientConfig = new SshClientConfig(host, 22, "centos", null, path); 36 | } 37 | 38 | SshClientsPool pool(int maxTotal, int maxIdle, long idleTime, long maxWaitMillis) { 39 | return new SshClientsPool(maxTotal, maxIdle, idleTime, maxWaitMillis); 40 | } 41 | 42 | @After 43 | public void destroy() { 44 | 45 | } 46 | 47 | @Test 48 | public void testSuccess(){ 49 | // 50 | SyncThreadPool threadPool = SyncThreadPool.newPool(); 51 | SshClientsPool clientsPool = pool(2, 2, 120*1000, 100*1000); 52 | Set clients = new HashSet(); 53 | for (int i = 0; i < 4; i++) { 54 | threadPool.addJob("task"+i, ()->{ 55 | SshClientWrapper client = clientsPool.client(clientConfig); 56 | clients.add(client); 57 | SshResponse response = client.executeCommand("echo '123'", 100); 58 | return response; 59 | }); 60 | } 61 | Map result = threadPool.run(); 62 | 63 | List objectsInPool = clientsPool.getObjects(clientConfig); 64 | assertArrayEquals(new Object[]{ 65 | 2, 4, 0, 2 66 | }, new Object[]{ 67 | clients.size(), 68 | result.keySet().size(), 69 | ((SshResponse)result.get("task1").getRtObject()).getCode(), 70 | objectsInPool.size() 71 | }); 72 | } 73 | 74 | 75 | /** 76 | * 当在池子中所有的client意外中断后,我们任然可以获取到可用的client 77 | */ 78 | @Test 79 | public void testDisconnectedIdleclientsSuccess(){ 80 | // 81 | SyncThreadPool threadPool = SyncThreadPool.newPool(); 82 | SshClientsPool clientsPool = pool(2, 2, 30*1000, 100*1000); 83 | Set clients = new HashSet(); 84 | for (int i = 0; i < 2; i++) { 85 | threadPool.addJob("task"+i, ()->{ 86 | SshClientWrapper client = clientsPool.client(clientConfig); 87 | clients.add(client); 88 | SshResponse response = client.executeCommand("echo '123'", 100); 89 | return response; 90 | }); 91 | } 92 | Map result = threadPool.run(); 93 | clients.forEach(item->{ 94 | item.disconnect(); 95 | }); 96 | 97 | threadPool.clearJobs(); 98 | for (int i = 0; i < 2; i++) { 99 | threadPool.addJob("task"+i, ()->{ 100 | SshClientWrapper client = clientsPool.client(clientConfig); 101 | SshResponse response = client.executeCommand("echo '123'", 100); 102 | return response; 103 | }); 104 | } 105 | result = threadPool.run(); 106 | List objectsInPool = clientsPool.getObjects(clientConfig); 107 | assertArrayEquals(new Object[]{ 108 | 2, 0, 0, 2l 109 | }, new Object[]{ 110 | result.keySet().size(), 111 | ((SshResponse)result.get("task1").getRtObject()).getCode(), 112 | ((SshResponse)result.get("task0").getRtObject()).getCode(), 113 | objectsInPool.stream().count() 114 | }); 115 | } 116 | 117 | /** 118 | * 如果客户端连接不上,那么是无法获取到client的 119 | */ 120 | @Test 121 | public void testUnreachableServerWillNotStayInPoolSuccess(){ 122 | // 123 | SyncThreadPool threadPool = SyncThreadPool.newPool(); 124 | SshClientsPool clientsPool = pool(2, 2, 30*1000, 100*1000); 125 | Set clients = new HashSet(); 126 | clientConfig.setUsername(clientConfig.getUsername()+"2"); 127 | for (int i = 0; i < 2; i++) { 128 | threadPool.addJob("task"+i, ()->{ 129 | SshClientWrapper client = clientsPool.client(clientConfig); 130 | clients.add(client); 131 | SshResponse response = client.executeCommand("echo '123'", 100); 132 | return response; 133 | }); 134 | } 135 | Map result = threadPool.run(); 136 | List objectsInPool = clientsPool.getObjects(clientConfig); 137 | assertArrayEquals(new Object[]{ 138 | 2, true, true, true 139 | }, new Object[]{ 140 | result.keySet().size(), 141 | result.get("task1").getState() == JobExecutorState.failed, 142 | result.get("task0").getState() == JobExecutorState.failed, 143 | objectsInPool==null || objectsInPool.size()==0 144 | }); 145 | } 146 | 147 | /** 148 | * 测试client会被池回收 149 | */ 150 | @Test 151 | public void testClientsWillBeRecycledSuccess(){ 152 | // 153 | SyncThreadPool threadPool = SyncThreadPool.newPool(); 154 | SshClientsPool clientsPool = pool(2, 2, 30*1000, 100*1000); 155 | Set clients = new HashSet(); 156 | for (int i = 0; i < 2; i++) { 157 | threadPool.addJob("task"+i, ()->{ 158 | SshClientWrapper client = clientsPool.client(clientConfig); 159 | clients.add(client); 160 | SshResponse response = client.executeCommand("echo '123'", 100); 161 | return response; 162 | }); 163 | } 164 | Map result = threadPool.run(); 165 | List objectsInPool = clientsPool.getObjects(clientConfig); 166 | assertArrayEquals(new Object[]{ 167 | 2, true, true, 2 168 | }, new Object[]{ 169 | result.keySet().size(), 170 | result.get("task1").getState() == JobExecutorState.successful, 171 | result.get("task0").getState() == JobExecutorState.successful, 172 | objectsInPool.size() 173 | }); 174 | 175 | try { 176 | Thread.sleep(60000); 177 | } catch (InterruptedException e) { 178 | e.printStackTrace(); 179 | } 180 | objectsInPool = clientsPool.getObjects(clientConfig); 181 | assertArrayEquals(new Object[]{ 182 | true 183 | }, new Object[]{ 184 | objectsInPool==null || objectsInPool.size()==0 185 | }); 186 | } 187 | 188 | @Test 189 | public void testSuccessWithCustomerPoolConfig(){ 190 | // 191 | SyncThreadPool threadPool = SyncThreadPool.newPool(); 192 | SshClientPoolConfig poolConfig = new SshClientPoolConfig(); 193 | poolConfig.setMaxTotalPerKey(1); // pool 中允许的最大对象数 194 | poolConfig.setMaxIdlePerKey(1); // pool中允许的最大空闲对象数 195 | poolConfig.setBlockWhenExhausted(true); // 当pool中已经达到最大值并无空闲,请求者将被阻塞MaxWaitMillis时间 196 | poolConfig.setMaxWaitMillis(1000L * 100); // 阻塞等待时间 197 | // 60秒清理 198 | poolConfig.setMinEvictableIdleTimeMillis(1000L * 100); // 后台清理idle过期对象 199 | poolConfig.setTimeBetweenEvictionRunsMillis(1000L * 100); // 后台清理idle过期对象的周期 200 | poolConfig.setTestOnBorrow(true); // 在租用时,validate对象 201 | poolConfig.setTestOnReturn(true); // 在归还时,validate对象 202 | poolConfig.setTestWhileIdle(true); // 在idle时,validate对象 203 | poolConfig.setJmxEnabled(false); // 禁止jmx 204 | 205 | poolConfig.setMaxTotal(1); 206 | SshClientsPool clientsPool = new SshClientsPool(poolConfig ); 207 | Set clients = new HashSet(); 208 | for (int i = 0; i < 4; i++) { 209 | threadPool.addJob("task"+i, ()->{ 210 | SshClientWrapper client = clientsPool.client(clientConfig); 211 | clients.add(client); 212 | SshResponse response = client.executeCommand("echo '123'", 100); 213 | return response; 214 | }); 215 | } 216 | Map result = threadPool.run(); 217 | 218 | List objectsInPool = clientsPool.getObjects(clientConfig); 219 | assertArrayEquals(new Object[]{ 220 | 1, 4, 0, 1 221 | }, new Object[]{ 222 | clients.size(), 223 | result.keySet().size(), 224 | ((SshResponse)result.get("task1").getRtObject()).getCode(), 225 | objectsInPool.size() 226 | }); 227 | clientsPool.clear(); 228 | } 229 | 230 | @Test 231 | public void testSuccessWithCustomerSshClient(){ 232 | // 233 | SyncThreadPool threadPool = SyncThreadPool.newPool(); 234 | 235 | SshClientPoolConfig poolConfig = new SshClientPoolConfig(2, 2, 30*1000, 100*1000); 236 | poolConfig.setSshClientImplClass(CustomerSshClient.class); 237 | 238 | SshClientsPool clientsPool = new SshClientsPool(poolConfig); 239 | Set clients = new HashSet(); 240 | for (int i = 0; i < 4; i++) { 241 | threadPool.addJob("task"+i, ()->{ 242 | SshClientWrapper client = clientsPool.client(clientConfig); 243 | clients.add(client); 244 | SshResponse response = client.executeCommand("echo '123'", 100); 245 | return response; 246 | }); 247 | } 248 | Map result = threadPool.run(); 249 | 250 | List objectsInPool = clientsPool.getObjects(clientConfig); 251 | assertArrayEquals(new Object[]{ 252 | 2, 4, true, 2 253 | }, new Object[]{ 254 | clients.size(), 255 | result.keySet().size(), 256 | ((SshResponse)result.get("task1").getRtObject()).getStdout().stream().collect(Collectors.joining("")).contains("success"), 257 | objectsInPool.size() 258 | }); 259 | clientsPool.clear(); 260 | } 261 | 262 | } 263 | 264 | 265 | -------------------------------------------------------------------------------- /src/test/java/com/github/test/multithread/JobExecutor.java: -------------------------------------------------------------------------------- 1 | package com.github.test.multithread; 2 | 3 | import java.util.function.Supplier; 4 | 5 | public class JobExecutor { 6 | private Object key; 7 | private JobExecutorState state; 8 | private Object rtObject; 9 | private Throwable throwable; 10 | private Supplier task; 11 | 12 | public boolean stateComplete(){ 13 | return state != JobExecutorState.created 14 | && state != JobExecutorState.running; 15 | 16 | } 17 | 18 | public boolean stateTimeout(){ 19 | return state == JobExecutorState.timeout; 20 | } 21 | 22 | public boolean stateSuccessful(){ 23 | return state == JobExecutorState.successful; 24 | } 25 | 26 | public Object getKey() { 27 | return key; 28 | } 29 | 30 | public void setKey(Object key) { 31 | this.key = key; 32 | } 33 | 34 | public JobExecutorState getState() { 35 | return state; 36 | } 37 | 38 | public void setState(JobExecutorState state) { 39 | this.state = state; 40 | } 41 | 42 | public Object getRtObject() { 43 | return rtObject; 44 | } 45 | 46 | public void setRtObject(Object rtObject) { 47 | this.rtObject = rtObject; 48 | } 49 | 50 | public Throwable getThrowable() { 51 | return throwable; 52 | } 53 | 54 | public void setThrowable(Throwable throwable) { 55 | this.throwable = throwable; 56 | } 57 | 58 | public Supplier getTask() { 59 | return task; 60 | } 61 | 62 | public void setTask(Supplier task) { 63 | this.task = task; 64 | } 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/com/github/test/multithread/JobExecutorState.java: -------------------------------------------------------------------------------- 1 | package com.github.test.multithread; 2 | 3 | public enum JobExecutorState { 4 | created, running, manaul_stop, successful, failed, timeout 5 | } 6 | -------------------------------------------------------------------------------- /src/test/java/com/github/test/multithread/SyncThreadPool.java: -------------------------------------------------------------------------------- 1 | package com.github.test.multithread; 2 | 3 | import java.util.Map; 4 | import java.util.concurrent.ConcurrentHashMap; 5 | import java.util.concurrent.CountDownLatch; 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.Future; 9 | import java.util.concurrent.TimeUnit; 10 | import java.util.function.Supplier; 11 | import java.util.stream.Collectors; 12 | 13 | import org.slf4j.Logger; 14 | import org.slf4j.LoggerFactory; 15 | import org.slf4j.MDC; 16 | 17 | 18 | /** 19 | * 20 | * @author jameswu 21 | * 22 | * thread pool 23 | * execute multithread and return result in main thread 24 | */ 25 | public class SyncThreadPool { 26 | private final static Logger logger = LoggerFactory.getLogger(SyncThreadPool.class); 27 | 28 | private Map jobs = new ConcurrentHashMap<>(); 29 | private Map> futures = new ConcurrentHashMap<>(); 30 | 31 | private int timeout; 32 | private TimeUnit timeunit; 33 | private SyncThreadPool(){ 34 | 35 | } 36 | 37 | public static SyncThreadPool newPool(){ 38 | return newPool(-1, TimeUnit.SECONDS); 39 | } 40 | 41 | public static SyncThreadPool newPool(int timeOut, TimeUnit timeunit){ 42 | SyncThreadPool pool = new SyncThreadPool(); 43 | pool.timeout = timeOut; 44 | pool.timeunit = timeunit; 45 | return pool; 46 | } 47 | 48 | /** 49 | * 50 | * threadpool.addJob("name", ()->{return value;}) 51 | * @param key 52 | * @param task 53 | */ 54 | public SyncThreadPool addJob(Object key, Supplier task){ 55 | JobExecutor jobExecutor = new JobExecutor(); 56 | jobExecutor.setState(JobExecutorState.created); 57 | jobExecutor.setTask(task); 58 | jobExecutor.setKey(key); 59 | this.jobs.put(key, jobExecutor); 60 | return this; 61 | } 62 | 63 | public void removeJob(Object key){ 64 | this.jobs.remove(key); 65 | } 66 | 67 | public void clearJobs(){ 68 | this.jobs.clear(); 69 | } 70 | 71 | public void stop(){ 72 | this.killAllThreads(); 73 | for(Object key: jobs.keySet()){ 74 | JobExecutor job = jobs.get(key); 75 | if(!job.stateComplete()){ 76 | job.setState(JobExecutorState.manaul_stop); 77 | } 78 | } 79 | } 80 | 81 | private void killAllThreads(){ 82 | this.futures.values().forEach(item->{ 83 | item.cancel(true); 84 | }); 85 | } 86 | 87 | /** 88 | * 89 | * throw Timeoutexception if all thread exceed timeout 90 | * return [jobid:executor] 91 | * executor: 92 | * [state:successful rtObject:nonnull] 执行成功 93 | * [state:failed rtObject:null throwable:nonnull] 执行过程中出错,如exeception 94 | * [state:timeout rtObejct:null throwable:timeoutExpcetion] job超时 95 | * [state:manual_stop rtObject:null throwable:null] 人工停止 96 | * 97 | */ 98 | public Map run(){ 99 | logger.debug("Sync thread pool begin running "+this.jobs.size()+" jobs"); 100 | ExecutorService executorService = Executors.newFixedThreadPool(jobs.size()); 101 | CountDownLatch latch = new CountDownLatch(jobs.size()); 102 | for(Object key: this.jobs.keySet()){ 103 | Future future = executorService.submit(new InnerRunnableTask(this.jobs.get(key), latch, MDC.getCopyOfContextMap())); 104 | this.futures.put(key, future); 105 | } 106 | executorService.shutdown(); 107 | try { 108 | if(this.timeout == -1){ 109 | latch.await(); 110 | }else{ 111 | latch.await(this.timeout, this.timeunit); 112 | } 113 | jobs.values().stream().filter(item->!item.stateComplete()).collect(Collectors.toList()).forEach(item->{ 114 | item.setState(JobExecutorState.timeout); 115 | }); 116 | } catch (InterruptedException e) { 117 | logger.error("threadpool interrupted exception", e); 118 | }finally { 119 | this.killAllThreads(); 120 | } 121 | return jobs; 122 | } 123 | 124 | } 125 | 126 | class InnerRunnableTask implements Runnable{ 127 | private final static Logger logger = LoggerFactory.getLogger(InnerRunnableTask.class); 128 | CountDownLatch latch; 129 | JobExecutor executor; 130 | Map logContextMap; 131 | 132 | InnerRunnableTask(JobExecutor executor, CountDownLatch latch, Map logContextMap){ 133 | this.latch = latch; 134 | this.executor =executor; 135 | this.logContextMap = logContextMap; 136 | this.executor.setState(JobExecutorState.created); 137 | } 138 | 139 | @Override 140 | public void run() { 141 | try{ 142 | if(this.logContextMap!=null) { 143 | MDC.setContextMap(this.logContextMap); 144 | } 145 | executor.setState(JobExecutorState.running); 146 | Object object = executor.getTask().get(); 147 | if(!executor.stateComplete()){ 148 | executor.setState(JobExecutorState.successful); 149 | executor.setRtObject(object); 150 | } 151 | }catch(Exception e){ 152 | logger.error("执行innerrunnabletask出错", e); 153 | if(!JobExecutorState.timeout.equals(executor.getState())) { 154 | executor.setState(JobExecutorState.failed); 155 | executor.setThrowable(e); 156 | } 157 | }finally { 158 | latch.countDown(); 159 | } 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /src/test/java/id_rsa: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEogIBAAKCAQEAhi9rGgINpjnZtnNWp/621aTcRiGQn8TQ4DqacKRAhOepeAKb 3 | vln+7XlulA/+lKddkAHo/YlKtiCnPChxptOzRjgjOhSNyiC9awrx6+IW3sonVUlu 4 | rXEb92xQZtPwAXTVGYDidMlpFra6WI4pTkn9d/pnBa8eRmJIKBTggs0TSo0mgNt1 5 | qQoyxgsbRBORHz03LSvTQ+wRZndYgA6BCsrdWzlAIRl0i4SURMpok6jnRbDD6yZt 6 | K8gqVGcpHHoQXwuZO9Z5t8R2hNdvcQxox3A5z8dvxcSc+1u3KnSBIL3fiUzs1Bbt 7 | YCnkcJsCqABj0Qk7V/QEvD6xhO+xZfHnXnr5UwIDAQABAoIBAFkU2dFmU5FrDnHl 8 | mD3nB5JF5EZauhyXGOQDg8wL/xk8gnE9qg9wx6D34MVC6QvfIGoeGoNWxdarTtrN 9 | LsZF/No9IoXIZ2ks2xNZLMogSnaVVg90tJlIYfycV7QsN0WAPoiz26MUo0OFA6kO 10 | l7V1wWQJRKOtYjfvv198nJ+5bXkryGuD66xNgAy6TRSbsguaGvcOWvdXXW0YDu/q 11 | o7Xut+l5rPUR8+lVODw23Wq4ecGHtits/lwh1he5Z3kwiCtBH9J1Vkh6CILITfpD 12 | XvUi5Y/V+Zpdqf1dkUJKZGr+eRDdzLaTuKb253lbPGo1cNE7InbhV9Q4bdJnx+EM 13 | ptvfJWECgYEA4+JKPAHE7YIChphBAbIkffRZyJ+L+S137DMNOqxur/jptOMMxJtL 14 | wsC8adKhp4OcC93zE17816afRnVW+tNLY0+ebve5j0SUdvbmBPxj/Z5JwXOmFeBG 15 | GBB7ENTc0Wf9uMdTso958danOaWhvHCpCVTHMlo36tzpwYvzMv3lDfUCgYEAlr2n 16 | /TxE/G68ws7W4BB1WIGP6tXJe9721Q8quUqBSFTcxL962mC3hsfp+cFMs2DSa4qa 17 | 09/DVMmyhPz++Ntde6N2bmn+VE/EV85Ud0ckRd/A5Bkqq5GRGVMBAFP1XceM1aUn 18 | r32JnersYm5pgnTaLwj3TwsbW582GfnV/O911ScCgYAd0DciDqEy6XVnInguvp2j 19 | lNpCMw1e9iHgVvV+HZ0FjK0WqzW6aw7IVerQdWwMvF0n+Eua1yyBEh14XFWazxXn 20 | 0jzKC8LNCY3a2oZRVKvIPHMtyfGKnyLMdyuCkjapds9s4zcB/19W0lxiYFb+jkav 21 | H9ZHsiHqqigy2BpjK3BSGQKBgH0qihz6OIYrJU3VFhMDV4zgODJUxnK21tpfN6iX 22 | tvXqkU5FeKtv97RQt3WNDjU8RjzJ4HnhqX3z5OoMcWMOM3eAacyQ8n/rpCsMxpIs 23 | XnAutSoxw36m5obSp1gwXuM52TVlZ6iOYwDfmty4tZ9/i9PWbBQUMulHMFMtYoQe 24 | CgetAoGAHCpqFcZAnLFCLxRr0+O20DORAVHUJSNJphOjb2UdTLM7jVY+Ns5nBbiB 25 | AzKutcHou34gtqazHFRlGz3jw3cOKqMTdu4Fj3JQroVgWPT0eQRfCHzbj4ET9yeF 26 | LTNMRj/KLbDaLBYEXEhR4ezNIlPJOUdVugZaxa9UUG6mqD6FMWY= 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /src/test/java/id_rsa.pub: -------------------------------------------------------------------------------- 1 | ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCGL2saAg2mOdm2c1an/rbVpNxGIZCfxNDgOppwpECE56l4Apu+Wf7teW6UD/6Up12QAej9iUq2IKc8KHGm07NGOCM6FI3KIL1rCvHr4hbeyidVSW6tcRv3bFBm0/ABdNUZgOJ0yWkWtrpYjilOSf13+mcFrx5GYkgoFOCCzRNKjSaA23WpCjLGCxtEE5EfPTctK9ND7BFmd1iADoEKyt1bOUAhGXSLhJREymiTqOdFsMPrJm0ryCpUZykcehBfC5k71nm3xHaE129xDGjHcDnPx2/FxJz7W7cqdIEgvd+JTOzUFu1gKeRwmwKoAGPRCTtX9AS8PrGE77Fl8edeevlT @weitu.mopaas.com --------------------------------------------------------------------------------