add GanymedUtil

This commit is contained in:
Looly 2020-12-11 02:45:22 +08:00
parent 4f6155c45a
commit 726b55a742
4 changed files with 139 additions and 1 deletions

View File

@ -19,6 +19,7 @@
* 【core 】 CollUtil.addAll增加判空pr#228@Gitee
* 【core 】 修正DateUtil.betweenXXX注释错误issue#I28XGW@Gitee
* 【core 】 增加NioUtil
* 【core 】 增加GanymedUtil
### Bug修复
* 【cache 】 修复Cache中get重复misCount计数问题issue#1281@Github

View File

@ -130,6 +130,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!-- 二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>

View File

@ -0,0 +1,130 @@
package cn.hutool.extra.ssh;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
/**
* Ganymed-SSH2封装http://www.ganymed.ethz.ch/ssh2/
*
* @author looly
* @since 5.5.3
*/
public class GanymedUtil {
/**
* 连接到服务器
*
* @param sshHost 主机
* @param sshPort 端口
* @return {@link Connection}
*/
public static Connection connect(String sshHost, int sshPort) {
Connection conn = new Connection(sshHost, sshPort);
try {
conn.connect();
} catch (IOException e) {
throw new IORuntimeException(e);
}
return conn;
}
/**
* 打开远程会话
*
* @param sshHost 主机
* @param sshPort 端口
* @param sshUser 用户名如果为null默认root
* @param sshPass 密码
* @return {@link Session}
*/
public static Session openSession(String sshHost, int sshPort, String sshUser, String sshPass) {
// 默认root用户
if (StrUtil.isEmpty(sshUser)) {
sshUser = "root";
}
final Connection connect = connect(sshHost, sshPort);
try {
connect.authenticateWithPassword(sshUser, sshPass);
return connect.openSession();
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
/**
* 执行Shell命令使用EXEC方式
* <p>
* 此方法单次发送一个命令到服务端不读取环境变量执行结束后自动关闭Session不会产生阻塞
* </p>
*
* @param session Session会话
* @param cmd 命令
* @param charset 发送和读取内容的编码
* @param errStream 错误信息输出到的位置
*/
public static String exec(Session session, String cmd, Charset charset, OutputStream errStream) {
final String result;
try {
session.execCommand(cmd, charset.name());
result = IoUtil.read(new StreamGobbler(session.getStdout()), charset);
// 错误输出
IoUtil.copy(new StreamGobbler(session.getStdout()), errStream);
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
close(session);
}
return result;
}
/**
* 执行Shell命令
* <p>
* 此方法单次发送一个命令到服务端自动读取环境变量执行结束后自动关闭Session不会产生阻塞
* </p>
*
* @param session Session会话
* @param cmd 命令
* @param charset 发送和读取内容的编码
* @param errStream 错误信息输出到的位置
*/
public static String execByShell(Session session, String cmd, Charset charset, OutputStream errStream) {
final String result;
try {
session.requestDumbPTY();
IoUtil.write(session.getStdin(), charset, true, cmd);
result = IoUtil.read(new StreamGobbler(session.getStdout()), charset);
if(null != errStream){
// 错误输出
IoUtil.copy(new StreamGobbler(session.getStdout()), errStream);
}
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
close(session);
}
return result;
}
/**
* 关闭会话
*
* @param session 会话通道
*/
public static void close(Session session) {
if (session != null) {
session.close();
}
}
}

View File

@ -417,7 +417,7 @@ public class JschUtil {
* @param cmd 命令
* @param charset 发送和读取内容的编码
* @param errStream 错误信息输出到的位置
* @return {@link ChannelExec}
* @return 执行结果内容
* @since 4.3.1
*/
public static String exec(Session session, String cmd, Charset charset, OutputStream errStream) {