mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 20:58:00 +08:00
add execByShell
This commit is contained in:
parent
11191b3cfe
commit
c97c76d49d
1461
CHANGELOG-v4.md
1461
CHANGELOG-v4.md
File diff suppressed because it is too large
Load Diff
@ -6,6 +6,8 @@
|
||||
## 5.2.6 (2020-03-26)
|
||||
|
||||
### 新特性
|
||||
* 【extra 】 JschUtil增加execByShell方法(issue#I1CYES@Gitee)
|
||||
|
||||
### Bug修复
|
||||
* 【extra 】 修复SpringUtil使用devtools重启报错问题
|
||||
|
||||
|
@ -6,7 +6,13 @@ import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.net.LocalPortGenerater;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.jcraft.jsch.*;
|
||||
import com.jcraft.jsch.Channel;
|
||||
import com.jcraft.jsch.ChannelExec;
|
||||
import com.jcraft.jsch.ChannelSftp;
|
||||
import com.jcraft.jsch.ChannelShell;
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Session;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -172,7 +178,7 @@ public class JschUtil {
|
||||
sshUser = "root";
|
||||
}
|
||||
|
||||
if(null == jsch){
|
||||
if (null == jsch) {
|
||||
jsch = new JSch();
|
||||
}
|
||||
|
||||
@ -343,7 +349,10 @@ public class JschUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行Shell命令
|
||||
* 执行Shell命令(使用EXEC方式)
|
||||
* <p>
|
||||
* 此方法单次发送一个命令到服务端,不读取环境变量,执行结束后自动关闭channel,不会产生阻塞。
|
||||
* </p>
|
||||
*
|
||||
* @param session Session会话
|
||||
* @param cmd 命令
|
||||
@ -375,6 +384,46 @@ public class JschUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行Shell命令
|
||||
* <p>
|
||||
* 此方法单次发送一个命令到服务端,自动读取环境变量,执行结束后自动关闭channel,不会产生阻塞。<br>
|
||||
* 此方法返回数据中可能
|
||||
* </p>
|
||||
*
|
||||
* @param session Session会话
|
||||
* @param cmd 命令
|
||||
* @param charset 发送和读取内容的编码
|
||||
* @return {@link ChannelExec}
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static String execByShell(Session session, String cmd, Charset charset) {
|
||||
final ChannelShell shell = openShell(session);
|
||||
// 开始连接
|
||||
shell.setPty(true);
|
||||
OutputStream out = null;
|
||||
InputStream in = null;
|
||||
final StringBuilder result = StrUtil.builder();
|
||||
try {
|
||||
out = shell.getOutputStream();
|
||||
in = shell.getInputStream();
|
||||
|
||||
out.write(StrUtil.bytes(cmd, charset));
|
||||
out.flush();
|
||||
|
||||
while (in.available() > 0) {
|
||||
result.append(IoUtil.read(in, charset));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
} finally {
|
||||
IoUtil.close(out);
|
||||
IoUtil.close(in);
|
||||
close(shell);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭SSH连接会话
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user