原生API
import lombok.extern.slf4j.Slf4j;
import com.jcraft.jsch.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* SSH 操作
* jsch 原始版本
* 参考:https://blog.51cto.com/u_16213371/7001770
*
* @author jason
*/
@Slf4j
public class SshClient {
public static void main(String[] args) {
Session session = null;
ChannelExec channelExec = null;
try {
// 创建一个JSch对象
JSch jsch = new JSch();
// 创建一个Session对象
session = jsch.getSession("root", "sshHost", 22);
// 设置用户名和密码
session.setPassword("xxx");
// 设置超时时间
session.setTimeout(0);
// 设置是否接受不可信的主机密钥
session.setConfig("StrictHostKeyChecking", "no");
// 连接
session.connect();
// 创建一个Channel对象
channelExec = (ChannelExec) session.openChannel("exec");
log.info("创建一个 Channel 对象");
// 发送需要执行的命令
String command = "free -h";
channelExec.setCommand(command);
log.info("执行命令:{}", command);
int exitStatus = channelExec.getExitStatus();
log.info("退出状态为:{}", exitStatus);
channelExec.connect();
log.info("连接 Channel 对象");
// 下面是得到的内容
log.info("获取控制台输出:");
InputStream in = channelExec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String buf;
while ((buf = reader.readLine()) != null) {
log.info(buf);
}
} catch (Exception e) {
log.error("异常", e);
} finally {
assert channelExec != null;
channelExec.disconnect();
log.info("channelExec close...");
session.disconnect();
log.info("session close...");
}
}
}
结果
13:22:41.191 [main] INFO com.example.job.ssh.SshClient - 创建一个 Channel 对象
13:22:41.193 [main] INFO com.example.job.ssh.SshClient - 执行命令:free -h
13:22:41.194 [main] INFO com.example.job.ssh.SshClient - 退出状态为:-1
13:22:41.462 [main] INFO com.example.job.ssh.SshClient - 连接 Channel 对象
13:22:41.462 [main] INFO com.example.job.ssh.SshClient - 获取控制台输出:
total used free shared buff/cache available
Mem: 29G 8.3G 1.9G 1.4G 19G 19G
Swap: 3.0G 307M 2.7G
13:22:41.496 [main] INFO com.example.job.ssh.SshClient - channelExec close...
13:22:41.499 [main] INFO com.example.job.ssh.SshClient - session close...
Hutool 封装版本
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.extra.ssh.JschUtil;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;
/**
* SSH 操作
* Hutool 版本
* 参考:https://www.hutool.cn/docs/#/extra/Jsch%E5%B0%81%E8%A3%85/Jsch%E5%B7%A5%E5%85%B7-JschUtil
*
* @author jason
*/
@Slf4j
public class EasySshClient {
public static void main(String[] args) {
Session session = null;
try {
// 新建会话
session = JschUtil.getSession("sshHost", 22, "root", "xxx");
log.info("新建会话");
// 发送需要执行的命令
String command = "free -h";
log.info("执行命令:{}", command);
String terminal = JschUtil.exec(session, command, CharsetUtil.CHARSET_UTF_8);
log.info("获取控制台输出:");
System.out.println(terminal);
} catch (Exception e) {
log.error("异常", e);
} finally {
JschUtil.close(session);
log.info("session close...");
}
}
}
结果
13:22:01.551 [main] INFO com.example.job.ssh.EasySshClient - 新建会话
13:22:01.553 [main] INFO com.example.job.ssh.EasySshClient - 执行命令:free -h
13:22:01.871 [main] INFO com.example.job.ssh.EasySshClient - 获取控制台输出:
total used free shared buff/cache available
Mem: 29G 8.3G 1.9G 1.4G 19G 19G
Swap: 3.0G 307M 2.7G
13:22:01.874 [main] INFO com.example.job.ssh.EasySshClient - session close...