具体思路就是通过连接服务器,然后执行shell脚本,将返回的数据进行处理。
1、maven引入jar包
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
2、具体实现代码
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/**
* @author UIHO-N5
* @create 2021/5/28 17:40
*/
@Slf4j
public class DistanceLinkUtil {
//分割器
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static Session session;
/**
* 创建连接
* @param user 用户名
* @param pwd 密码
* @param host ip、域名
* @return 是否连接
*/
private static boolean connect(String user, String pwd, String host) {
JSch jsch = new JSch();
try {
session = jsch.getSession(user, host, 22);
session.setPassword(pwd);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(10000);
} catch (JSchException e) {
e.printStackTrace();
log.error("connect error !");
return false;
}
return true;
}
/**
* 远程连接Linux 服务器 执行相关的命令
* @param command 执行的脚本
* @param user 用户名
* @param pwd 密码
* @param host ip/域名
*/
public static String runShell(String command, String user, String pwd, String host) {
if (!connect(user, pwd, host)) {
return null;
}
StringBuilder stringBuffer = null;
BufferedReader reader = null;
Channel channel = null;
try {
stringBuffer = new StringBuilder();
//管道类型多种,session、shell、exec、x11、auth-agent@openssh.com、direct-tcpip、forwarded-tcpip、sftp、subsystem、
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
InputStream in = channel.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
String buf;
while ((buf = reader.readLine()) != null) {
//舍弃PID 进程信息
if (buf.contains("PID")) {
break;
}
stringBuffer.append(buf.trim()).append(LINE_SEPARATOR);
}
} catch (IOException | JSchException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (channel != null) {
channel.disconnect();
}
session.disconnect();
}
return stringBuffer.toString();
}
public static void main(String[] args) {
String myCommand = "free -m";
String result = runShell(myCommand, "root", "123456",
"127.0.0.1");
log.info(result);
}
}
剩下的就是对结果经行处理了,运行结果,
Connected to the target VM, address: '127.0.0.1:62592', transport: 'socket'
total used free shared buff/cache available
Mem: 985 477 75 2 431 353
Swap: 0 0 0
3、如果执行本地脚本
/**
* 直接在本地执行 shell
* @param command 脚本
*/
public static String runShell(String command) {
Runtime runtime = Runtime.getRuntime();
StringBuilder stringBuffer = null;
BufferedReader reader;
Process process;
try {
stringBuffer = new StringBuilder();
process = runtime.exec(command);
InputStream inputStream = process.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
String buf;
while ((buf = reader.readLine()) != null) {
//舍弃PID 进程信息
if (buf.contains("PID")) {
break;
}
stringBuffer.append(buf.trim()).append(LINE_SEPARATOR);
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return stringBuffer.toString();
}
感谢博主月慕向阳
参考地址:https://blog.csdn.net/weixin_41796956/article/details/104007899