mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix ssh bug
This commit is contained in:
parent
4e0cf4cb72
commit
c7dcf6e6e9
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
### 新特性
|
### 新特性
|
||||||
### Bug修复
|
### Bug修复
|
||||||
|
* 【extra】 修复遗留的getSession端口判断错误(issue#594@Github)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ Hutool是一个小而全的Java工具类库,通过静态方法封装,降低
|
|||||||
|
|
||||||
Hutool中的工具方法来自于每个用户的精雕细琢,它涵盖了Java开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当;
|
Hutool中的工具方法来自于每个用户的精雕细琢,它涵盖了Java开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当;
|
||||||
|
|
||||||
Hutool是项目中“util”包友好的替代,它节省了我们对项目中公用类和公用工具方法的封装时间,使开发专注于业务,同时可以最大限度的避免封装不完善带来的bug。
|
Hutool是项目中“util”包友好的替代,它节省了开发人员对项目中公用类和公用工具方法的封装时间,使开发专注于业务,同时可以最大限度的避免封装不完善带来的bug。
|
||||||
|
|
||||||
### Hutool名称的由来
|
### Hutool名称的由来
|
||||||
|
|
||||||
|
@ -119,28 +119,13 @@ public class JschUtil {
|
|||||||
* @since 4.5.2
|
* @since 4.5.2
|
||||||
*/
|
*/
|
||||||
public static Session createSession(String sshHost, int sshPort, String sshUser, String sshPass) {
|
public static Session createSession(String sshHost, int sshPort, String sshUser, String sshPass) {
|
||||||
Assert.notEmpty(sshHost, "SSH Host must be not empty!");
|
|
||||||
Assert.isTrue(sshPort < 0, "SSH Host must be not empty!");
|
|
||||||
|
|
||||||
// 默认root用户
|
|
||||||
if (StrUtil.isEmpty(sshUser)) {
|
|
||||||
sshUser = "root";
|
|
||||||
}
|
|
||||||
|
|
||||||
final JSch jsch = new JSch();
|
final JSch jsch = new JSch();
|
||||||
Session session;
|
final Session session = createSession(jsch, sshHost, sshPort, sshUser);
|
||||||
try {
|
|
||||||
session = jsch.getSession(sshUser, sshHost, sshPort);
|
|
||||||
} catch (JSchException e) {
|
|
||||||
throw new JschRuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StrUtil.isNotEmpty(sshPass)) {
|
if (StrUtil.isNotEmpty(sshPass)) {
|
||||||
session.setPassword(sshPass);
|
session.setPassword(sshPass);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
|
|
||||||
session.setConfig("StrictHostKeyChecking", "no");
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,19 +141,43 @@ public class JschUtil {
|
|||||||
* @since 5.0.0
|
* @since 5.0.0
|
||||||
*/
|
*/
|
||||||
public static Session createSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase) {
|
public static Session createSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase) {
|
||||||
|
Assert.notEmpty(privateKeyPath, "PrivateKey Path must be not empty!");
|
||||||
|
|
||||||
|
final JSch jsch = new JSch();
|
||||||
|
try {
|
||||||
|
jsch.addIdentity(privateKeyPath, passphrase);
|
||||||
|
} catch (JSchException e) {
|
||||||
|
throw new JschRuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return createSession(jsch, sshHost, sshPort, sshUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个SSH会话,重用已经使用的会话
|
||||||
|
*
|
||||||
|
* @param jsch {@link JSch}
|
||||||
|
* @param sshHost 主机
|
||||||
|
* @param sshPort 端口
|
||||||
|
* @param sshUser 用户名,如果为null,默认root
|
||||||
|
* @return {@link Session}
|
||||||
|
* @since 5.0.3
|
||||||
|
*/
|
||||||
|
public static Session createSession(JSch jsch, String sshHost, int sshPort, String sshUser) {
|
||||||
Assert.notEmpty(sshHost, "SSH Host must be not empty!");
|
Assert.notEmpty(sshHost, "SSH Host must be not empty!");
|
||||||
Assert.isTrue(sshPort > 0, "SSH port must be > 0");
|
Assert.isTrue(sshPort > 0, "SSH port must be > 0");
|
||||||
Assert.notEmpty(privateKeyPath, "PrivateKey Path must be not empty!");
|
|
||||||
|
|
||||||
// 默认root用户
|
// 默认root用户
|
||||||
if (StrUtil.isEmpty(sshUser)) {
|
if (StrUtil.isEmpty(sshUser)) {
|
||||||
sshUser = "root";
|
sshUser = "root";
|
||||||
}
|
}
|
||||||
|
|
||||||
final JSch jsch = new JSch();
|
if(null == jsch){
|
||||||
|
jsch = new JSch();
|
||||||
|
}
|
||||||
|
|
||||||
Session session;
|
Session session;
|
||||||
try {
|
try {
|
||||||
jsch.addIdentity(privateKeyPath, passphrase);
|
|
||||||
session = jsch.getSession(sshUser, sshHost, sshPort);
|
session = jsch.getSession(sshUser, sshHost, sshPort);
|
||||||
} catch (JSchException e) {
|
} catch (JSchException e) {
|
||||||
throw new JschRuntimeException(e);
|
throw new JschRuntimeException(e);
|
||||||
@ -176,6 +185,7 @@ public class JschUtil {
|
|||||||
|
|
||||||
// 设置第一次登录的时候提示,可选值:(ask | yes | no)
|
// 设置第一次登录的时候提示,可选值:(ask | yes | no)
|
||||||
session.setConfig("StrictHostKeyChecking", "no");
|
session.setConfig("StrictHostKeyChecking", "no");
|
||||||
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- versions -->
|
<!-- versions -->
|
||||||
<poi.version>4.1.0</poi.version>
|
<poi.version>4.1.1</poi.version>
|
||||||
<xerces.version>2.12.0</xerces.version>
|
<xerces.version>2.12.0</xerces.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.oshi</groupId>
|
<groupId>com.github.oshi</groupId>
|
||||||
<artifactId>oshi-core</artifactId>
|
<artifactId>oshi-core</artifactId>
|
||||||
<version>4.0.0</version>
|
<version>4.1.0</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
Loading…
Reference in New Issue
Block a user